用于清理 CSV 文件的非常簡(jiǎn)單的查找和替換腳本。這以前有效,但現(xiàn)在我遇到了幾個(gè)奇怪的錯(cuò)誤。它接收一個(gè) csv (Out.csv),找到一個(gè)字符串find_str并將其替換為replace_str.運(yùn)行:python3 cleanup.ph -i Out.csv給出下面粘貼的錯(cuò)誤。劇本: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)錯(cuò)誤: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
用于編輯 csv 的簡(jiǎn)單“查找和替換”Python 腳本出現(xiàn)錯(cuò)誤
鴻蒙傳說(shuō)
2022-12-27 15:24:18