您的位置:首页 > 编程语言 > Python开发

Python 正则表达式改变csv文件的分隔符

2014-12-16 10:36 197 查看
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"          《====这里没有对齐??
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: