【发布时间】:2020-02-11 02:28:33
【问题描述】:
我正在打开一个.tsv 文件并使用一些正则表达式更改其内容:
with open("test.tsv") as tsvfile:
tsvreader = csv.reader(tsvfile, delimiter="\t")
for line in tsvreader:
#print(line[2])
match = re.search('(\w*[А-Я]\w*[А-Я]\w*)|(\d)|(%|$|€)', line[2])
if match:
print(line[2])
如何将修改后的内容保存到另一个.tsv 文件中?
更新: 我需要保存整行,而不仅仅是行[2]
【问题讨论】:
-
所以只需在其中使用另一个上下文管理器 - 这次打开
with open("test.tsv", "w") as f:并写入它。 -
另外,我认为你会想像
\$一样逃避$。