我只希望所有输出都在文本文件中
def load_comments(self, mat):
for item in mat["items"]:
comment = item["snippet"]["topLevelComment"]
author = comment["snippet"]["authorDisplayName"]
text = comment["snippet"]["textDisplay"]
if 'replies' in item.keys():
for reply in item['replies']['comments']:
rauthor = reply['snippet']['authorDisplayName']
rtext = reply["snippet"]["textDisplay"]
print("{}".format(rtext))
print("{}".format(text))
当我在运行代码时尝试简单地将> filename.txt放入时,它仅打印一些行,而不是所有行。但是,当我尝试在控制台上运行它时,它将打印所有注释。
答案 0 :(得分:0)
假设输出是这部分:
print("{}".format(text))
只需将其替换为:
with open('filename.txt', 'w+') as file:
file.write("{}".format(text))