簡體   English   中英

字符串中的特殊字符:Awk中的串聯

[英]special characters in strings: concatenation in Awk

我一直試圖將一個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 }'打印出來時, $ cat customer.csv | awk -F"," '(NR==3621789){ print $17 }' ,mysql警告中的行之一,我仍然看到一個13字符的字符串。

但是,當我嘗試以下操作時,似乎有一些隱藏字符。 有什么建議嗎? 謝謝。

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

這是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]應該始終為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
>>>>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM