为了账号安全,请及时绑定邮箱和手机立即绑定

用于编辑 csv 的简单“查找和替换”Python 脚本出现错误

用于编辑 csv 的简单“查找和替换”Python 脚本出现错误

鸿蒙传说 2022-12-27 15:24:18
用于清理 CSV 文件的非常简单的查找和替换脚本。这以前有效,但现在我遇到了几个奇怪的错误。它接收一个 csv (Out.csv),找到一个字符串find_str并将其替换为replace_str.运行:python3 cleanup.ph -i Out.csv给出下面粘贴的错误。剧本:import re# open your csv and read as a text stringwith open('Out.csv', 'r') as f:    my_csv_text = f.read()find_str = 'first published'replace_str = ' '# substitutenew_csv_str = re.sub(find_str, replace_str, my_csv_text)# open new file and savenew_csv_path = './my_new_csv.csv' # or whatever path and name you wantwith open(new_csv_path, 'w') as f:    f.write(new_csv_str)错误:Traceback (most recent call last):  File "cleanup.py", line 11, in <module>    new_csv_str = re.sub(find_str, replace_str, my_csv_text)  File "/usr/lib/python3.7/re.py", line 192, in sub    return _compile(pattern, flags).sub(repl, string, count)  File "/usr/lib/python3.7/re.py", line 286, in _compile    p = sre_compile.compile(pattern, flags)  File "/usr/lib/python3.7/sre_compile.py", line 764, in compile    p = sre_parse.parse(p, flags)  File "/usr/lib/python3.7/sre_parse.py", line 924, in parse    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)  File "/usr/lib/python3.7/sre_parse.py", line 420, in _parse_sub    not nested and not items))  File "/usr/lib/python3.7/sre_parse.py", line 813, in _parse    source.tell() - start)re.error: missing ), unterminated subpattern at position 1
查看完整描述

1 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

我在 find_str 中使用了一个 (。由于某种原因导致它不起作用。谢谢你们!!!



查看完整回答
反对 回复 2022-12-27
  • 1 回答
  • 0 关注
  • 197 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号