Python 3.x 如何在python中使多行文本文件成为数据帧?
有混乱的文本文件,需要将其转换为csv文件格式,如何实现,例如: 你好,这是第一线。 这是两条线都是一条原始线 你好,这是第二线。 这是两条线都是一条原始线Python 3.x 如何在python中使多行文本文件成为数据帧?,python-3.x,dataframe,nlp,Python 3.x,Dataframe,Nlp,有混乱的文本文件,需要将其转换为csv文件格式,如何实现,例如: 你好,这是第一线。 这是两条线都是一条原始线 你好,这是第二线。 这是两条线都是一条原始线 如何将多行文本(如段落)转换为数据框或cv格式您可以先读取行,然后将列表传递到数据框: 示例文件: sample.txt: hello this is first line. this is both lines are one raw. hello this is second line. this is both lines are o
如何将多行文本(如段落)转换为数据框或cv格式您可以先读取行,然后将列表传递到数据框: 示例文件:
sample.txt
:
hello this is first line. this is both lines are one raw.
hello this is second line. this is both lines are one raw.
代码:
将熊猫作为pd导入
#阅读这些行并创建一个数据帧
以open(“/content/sample.txt”,“r”)作为文件句柄:
df=pd.DataFrame(文件\u handle.readlines())
#将数据帧保存到CSV
df.to_csv(“output/file.csv”)