python - 将文件的格式化内容保存到Python中的另一个文件
问题描述
我正在打开一个.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") as tsvfile, open("new_test.tsv", "a") as new_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:
new_tsvfile.write(line[2] + "\n")
应该工作,但我没有测试它。
推荐阅读
- django - 我是 graphql 的新手,我发现在 django-graphql 中使用来自两个不同应用程序的模式有困难?
- java - 使用 MongoDB 在 Spring Boot 中将海量数据写入文件快速生成 Excel/XLSX
- node.js - 如何让nodejs和react使用不同的端口
- graph - 我需要一些“已知集团规模图”数据集进行实验。是否有任何开源或网站,我可以在哪里找到它们?
- ruby-on-rails - 部署后的 ActionController::InvalidAuthenticityToken
- php - php删除空格、逗号并插入数据库
- php - 禁用 gzip 压缩(安装 ext/zlib)
- html - 我可以使用相同的复选框 hack 触发 2 个动作吗?使用 CSS
- genexus - GX 15 U6 Java - OpenAPI(重新)生成
- c# - 将动态生成的输入列表表单数据从视图传递到asp.net中的控制器