python 中如何将dfcontent_cutted输出到txt
文件中?
可以使用 pandas 的 to_csv() 方法将 DataFrame 中的数据保存到 CSV 文件中,也可以使用 to_excel() 方法将数据保存到 Excel 文件中。另外,还可以使用 Python 内置的 open() 函数将数据保存到文本文件中。
以下是使用 open() 函数将 DataFrame 中的数据保存到文本文件的示例代码:
# 假设 df 是一个 DataFrame 对象,包含 "content_cutted" 列
with open("output.txt", "w", encoding="utf-8") as f:
for content in df["content_cutted"]:
f.write(content + "\n")
将上述代码保存到 Python 文件中并运行,即可将 df 中的 "content_cutted" 列的数据输出到 output.txt 文件中。其中,"w" 表示以写入模式打开文件,"utf-8" 表示使用 UTF-8 编码。如果文件不存在,open() 函数会自动创建一个新文件。
原文地址: https://www.cveoy.top/t/topic/bsqj 著作权归作者所有。请勿转载和采集!