简体   繁体   English

Python Panda如何将csv中的每个单元格写入文本文件中的一行

[英]Python panda how to write each cell in csv to one line in text file

Hi I am using Python and Panda to open a csv file and tries to write it into a text file. 嗨,我正在使用Python和Panda打开一个csv文件,并尝试将其写入文本文件。

There is a cell appears like: 出现一个像这样的单元格:

$AAL:
New Insider Filing on
EVP People and Communications
ELISE R EBERWEIN:

I want to make it like 我想让它像

$AAL: New Insider Filing on EVP People and Communications ELISE R EBERWEIN:

so basically for that cell to write it into one line in text file. 因此基本上,该单元格可以将其写入文本文件的一行中。

I have opened my file in panda: 我已经在熊猫打开文件了:

text = pd.read_csv("my_file.csv", encoding="UTF-8") 文字= pd.read_csv(“ my_file.csv”,编码=“ UTF-8”)

and write it to a text file: 并将其写入文本文件:

text.to_csv("output.txt", index=false) text.to_csv(“ output.txt”,index = false)

is there any thing i could do in between these two command in panda to make the cell to write into a text file in one line? 我可以在这两个命令之间做任何事情来使单元格在一行中写入文本文件吗?

The full table is: 完整表为:

https://i.stack.imgur.com/we3jE.jpg https://i.stack.imgur.com/we3jE.jpg

Try this to replace the linebreaks '\\n' in the line of interest with an empty space: 尝试执行以下操作,将感兴趣行中的换行符'\\ n'替换为空白:

df = pd.read_csv('file1.csv', encoding='UTF-8')
df['V3'] = df['V3'].apply(lambda x: x.replace('\n', ' '))
df.to_csv('out.csv', index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM