考虑以下Python代码:
with open('reviews.txt', 'r') as f:
reviews = f.read()
with open('labels.txt', 'r') as f:
labels = f.read()
我想将两个不同的文件开口合并在一行中。这样做的最佳方式是什么?
答案 0 :(得分:1)
如果用逗号分隔,可以合并多个open
命令:
with open('reviews.txt', 'r') as f1, open('labels.txt', 'r') as f2:
reviews = f1.read()
labels = f2.read()