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 }'
$ 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','')
string[0:12] should always be 12 characters. Maybe you'd better step through your program with pudb or similar.
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
>>>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.