【问题标题】:Unable to separate text data in csv. (Separate text with @ so that it becomes two columns)无法分隔 csv 中的文本数据。 (用@分隔文本,使其变成两列)
【发布时间】:2020-10-08 11:41:30
【问题描述】:

根据 Gran 的说法,该公司没有将所有生产转移到俄罗斯的计划,尽管该公司正在那里发展。@neutral

以上是正文,我想用@分隔,这样会产生两列

data = pd.read_csv(r'F:\Sentences_50Agree.csv', sep='@', header=None)

我尝试了上述方法,但它不起作用。它只显示 1 列的总文本,包括 @neutral

【问题讨论】:

  • 无法重现,如果我用上面的句子创建文件,运行你的代码,我会得到两列的数据框。
  • 你能把代码贴出来吗?会有帮助的
  • 我发布了我使用的代码。

标签: python-3.x pandas dataframe csv delimiter


【解决方案1】:
import pandas as pd
from io import StringIO


s = 'According to Gran, the company has no plans to move all production to Russia, although that is where the company is growing .@neutral'
print( pd.read_csv(StringIO(s), sep='@', header=None) )

打印:

                                                   0        1
0  According to Gran, the company has no plans to...  neutral

或带文件:

print( pd.read_csv('file.txt', sep='@', header=None) )

【讨论】:

  • 但是总数据帧怎么办??有 1000 行包含这样的文本。
  • @NithinReddy 我不明白。 “总数据框”是什么意思?
  • 你最后一个正在工作,最好添加参数'encoding="ISO-8859-1"'
猜你喜欢
  • 2021-12-29
  • 2010-10-03
  • 1970-01-01
  • 2016-05-25
  • 1970-01-01
  • 2014-03-18
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多