用于编辑CSV的简单“查找并替换” Python脚本给出了错误

问题描述 投票:0回答:1

一个非常简单的查找和替换脚本,用于清理CSV文件。这以前曾奏效,但现在我遇到了一些奇怪的错误。

它接收一个csv(Out.csv),找到一个字符串find_str,并将其替换为replace_str

运行:python3 cleanup.ph -i Out.csv给出了下面粘贴的错误。

脚本:

import re

# open your csv and read as a text string
with open('Out.csv', 'r') as f:
    my_csv_text = f.read()

find_str = 'first published'
replace_str = ' '

# substitute
new_csv_str = re.sub(find_str, replace_str, my_csv_text)

# open new file and save
new_csv_path = './my_new_csv.csv' # or whatever path and name you want
with 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
python csv
1个回答
0
投票

我在find_str中使用(由于某种原因,使其无法正常工作。谢谢你们!!

© www.soinside.com 2019 - 2024. All rights reserved.