【问题标题】:Processing text in csv document处理 csv 文档中的文本
【发布时间】:2017-06-12 01:23:11
【问题描述】:

我正在对一些 csv 文档进行一些文本分析。但是我的 csv 文档有几个句子,我不感兴趣,所以我想创建一个 python 代码来分析这个 csv 文档,只留下包含超过 5 个单词的句子供我分析,但是我不知道从哪里开始编写我的代码并需要一些帮助。

示例:

输入文件 enter image description here

输出文档 enter image description here

【问题讨论】:

  • Google for "python csv tutorial" - 您将了解 csv 模块以及如何将 csv 文件的内容加载到您的程序中。完成后,您将能够过滤掉那些不符合您设置的任何条件的行。
  • 也许可以先举一个输入数据的例子和你想得到的输出。

标签: python csv text-processing


【解决方案1】:

这应该可以工作(使用 Python 3.5):

lines = []
finalLines = []
toRemove = ['a', 'in', 'the']

with open('export.csv') as f:
    lines.append(f.readlines())

for line in lines:
    temp = list(csv.reader(line))
    sentence = ''
    for word in temp[0][0].split():
        if (word not in toRemove):
            sentence = sentence + ' ' + word
    finalLines.append(sentence.strip())

print(finalLines)

【讨论】:

    【解决方案2】:

    如果您使用 pandas(广泛用于数据操作的 Python 库),您可以轻松高效地完成工作。以下是 Pandas 官方文档的链接:

    http://pandas.pydata.org/pandas-docs/stable/

    注意:Pandas 具有读取 csv 文件的内置函数。您可以使用“skiprow”参数来跳过您不想要的内容或应用正则表达式来过滤文本。

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 2020-06-27
      • 1970-01-01
      • 2015-09-13
      • 2017-12-04
      • 1970-01-01
      • 2011-03-16
      • 2021-07-21
      • 2013-07-09
      相关资源
      最近更新 更多