【发布时间】:2021-06-17 08:20:33
【问题描述】:
我在 csv 文件中有如下数据:
| Name | Text |
|---|---|
| Apple | sweet fruit |
| Orange | citrus fruit |
| Strawberry | citrus fruit |
| eggplant | vegetable |
| lemon | sour |
我想将其转换为文本文件,两列之间有分隔符:
Apple <EOL> sweet fruit
Orange <EOL> cirtus fruit
Strawberry <EOL> citrus fruit
eggplant <EOL> vegetable
lemon <EOL> sour
为此,我将代码编写为:
df = open("../data.csv", "r")
df = '<EOL>'.join([i for i in df])
df = df.replace(",", " ")
print(df)
为此,我得到如下输出:
Name Text
<EOL>Apple sweet fruit
<EOL>Orange cirtus fruit
<EOL>Strawberry citrus fruit
<EOL>eggplant vegetable
<EOL>lemon sour
我尝试使用 skipinitialspace = True.. 但这会引发错误。有什么办法可以解决这个问题?
【问题讨论】: