0

I've been trying to load a csv file into mysql, and keep getting the data truncated warning for the last field in the csv.

The data is prepped with python, and I make sure that the string of the last field has length 13 (the declared field length in CREATE TABLE):

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

Any which way I measure len(cleanField( row[ 17 ] )[0:12]), it's 13. When I print it out using $ cat customer.csv | awk -F"," '(NR==3621789){ print $17 }', one of the rows in the mysql warning, I still see a 13-char string.

But when I try the following, there seems to be a hint of hidden character. Any advice? Thanks.

$ cat customer.csv | awk -F"," '(NR==3621789){ print "<" $17 ">" }'
>PRSP_CATS_CO

Here's 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','')
4

1 回答 1

1

string[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
>>>>
于 2013-04-30T00:35:11.480 回答