字符串中的特殊字符:在

2024-10-01 17:30:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试将一个csv文件加载到mysql中,并一直收到csv中最后一个字段的数据截断警告。在

数据是用python准备的,我确保最后一个字段的字符串长度为13(CREATE TABLE中声明的字段长度):

cleanField( row[ 17 ] )[0:12]

不管用哪种方法测量len(cleanField( row[ 17 ] )[0:12]),都是13。当我使用$ cat customer.csv | awk -F"," '(NR==3621789){ print $17 }'(mysql警告中的一行)打印出来时,我仍然看到一个13个字符的字符串。在

但当我尝试下面这些时,似乎有一点隐藏的性格。有什么建议吗?谢谢。在

^{pr2}$

以下是cleanField:

def cleanField(x):
    x = re.sub( ' +' , ' ' , x )
    try:
        x.decode('ascii')
    except UnicodeDecodeError:
        x = unicode( x , "UTF-8")
        x = unicodedata.normalize('NFKD', x ).encode('ascii', 'ignore')
    else:
        pass
    # " ".join(x.split())
    return x.replace(',','').replace('"','').replace("'",'').replace('\t','').replace('\n','').replace('\\','').replace('\s','')

Tags: 文件csv数据方法字符串声明警告len
1条回答
网友
1楼 · 发布于 2024-10-01 17:30:58

字符串[0:12]应始终为12个字符。也许你最好用pudb或类似的方法来完成你的计划。在

dstromberg@zareason ~ $ /usr/local/pypy-1.9/bin/pypy
Python 2.7.2 (341e1e3821ff, Jun 07 2012, 15:40:31)
[PyPy 1.9.0 with GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``how to construct the blackhole
interpreter: we reuse the tracing one, add lots of ifs and pray''
>>>> print '01234567890123456789'[0:12]
012345678901
>>>> print(len('01234567890123456789'[0:12]))
12
>>>>

相关问题 更多 >

    热门问题