1. 程式人生 > >Python 正則表示式改變csv檔案的分隔符

Python 正則表示式改變csv檔案的分隔符

import re
subject = '''
aaa,b b,"""c"" cc"
1,,"333, three,
still more threes"
'''

result = ""
reobj = re.compile(r'''(,|\r?\n|^)([^",\r\n]+|"(?:[^"]|"")*")?''')
for matchobj in reobj.finditer(subject):
    if matchobj.group(1) == ",":
        if matchobj.group(2) is None:
            result += "\t" + ""
        else:
            result += "\t" + matchobj.group(2)
    else:
        result += matchobj.group()

print result
aaa     b b     """c"" cc"  《===雙引號沒有去掉??有沒有高人可以指點一下
1               "333, three,
still more threes"          《====這裡沒有對齊??