有些句子存储在双引号中,有些句子存储在python中的csv文件中

时间:2018-06-20 07:49:52

标签: python python-3.x csv

import csv
data = open("/home/arushi/PycharmProjects/.../F1.csv", "r")
csvReader = csv.reader(data,
                   quotechar='"',
                   delimiter=(','),
                   quoting =csv.QUOTE_ALL,
                   skipinitialspace=True,
                   escapechar='\\')
header = next(csvReader)
MIndex = header.index("Message")
Messages = []
for row in csvReader:
   m = row[MIndex]
   Messages.append(m)
print(Messages)

/ home / arushi /...../ try9.py

  

['嗨。你好吗?我很好。那你呢?”,“我也很好。”,   “很好听。”,“让我们讨论昨晚的聚会。”,“不,我   不要以为那是健康的。”,“我同意他。”,“我不   认为我们在同一页上。”,'我认为我们应该实际讨论   以便清除所有事物。”,“甚至可能会弄乱事物   更多。”,“最好整理一下。”,“否则,将会有   很多误会..”,“好吧。”,“那就是精神!”,   “这里也一样。我认为很好。”,“最后,我们都处于同一状态   网页。”,“我很高兴我们仍然是朋友。”,“我不想   失去你们。”,“我们将永远是朋友。”,“是的”]

以退出代码0结束的过程

期望的输出:

  

[“嗨,你好吗?”,“我很好。你呢?”,“我也很好。”,   “很高兴听到。”,“让我们讨论昨晚的聚会。”,“不,我   不要以为那会健康。“,”我同意他的观点。“,”我不同意   认为我们在同一页上。“,”我认为我们应该实际讨论   以便清除所有事物。”,“甚至可能弄乱事物   更多。”,“最好整理一下。”,“否则,将会有   很多误会..“,”好吧。“,”那是精神!“,   “这里也一样。我认为很好。”,“最后,我们都在同一位置   页面。”,“我很高兴我们仍然是朋友。”,“我不想   失去你们。”,“我们将永远成为朋友。”,“是的”]

我想从包含英语句子的csv文件中读取一列到列表中。

我的csv文件包含7种不同类型的列,具有20个条目。

我应该更改文件格式吗?如果是,那么哪种格式支持文本,日期,时间,数字和字符。

我正在8GB-64位笔记本电脑上使用ubuntu 16.04。

1 个答案:

答案 0 :(得分:1)

您的实际输出和预期输出在所有重要方面都相同。

>>> a = ['Hi. How are you?', 'I am fine. What about you?', 'I am also good.', 'Good to hear that.', "Let's discuss last night's party.", "No. I don't think that will be healthy.", 'I agree with him.', "I don't think we are on the same page.", 'I think we should actually discuss it so that things get cleared.', 'Things may get messed up even more.', "It's better to sort out things.", 'Ya otherwise there will be a lot of misunderstanding..', 'Okay fine.', "That's the spirit!", "Same here. I think it's fine.", 'Finally we are all on the same page.', 'I am just happy we are all still friends.', "I don't want to lose you guys.", 'We will be friends forever.', 'Yeah']
>>> b = ["Hi. How are you?","I am fine. What about you?", "I am also good.", "Good to hear that.", "Let's discuss last night's party.", "No. I don't think that will be healthy.", "I agree with him.", "I don't think we are on the same page.", "I think we should actually discuss it so that things get cleared.", "Things may get messed up even more.", "It's better to sort out things.", "Ya otherwise there will be a lot of misunderstanding..", "Okay fine.", "That's the spirit!", "Same here. I think it's fine.", "Finally we are all on the same page.", "I am just happy we are all still friends.", "I don't want to lose you guys.", "We will be friends forever.", "Yeah"]
>>> a == b
True

完成时

print(Messages)

您依赖于Python内置的字符串列表表示形式。在简单情况下,它选择'胜过"。仅在"No. I don't think that will be healthy."之类的情况下,它才使用双引号。如果您想在屏幕上查看其他格式的字符串列表,则必须自己进行格式化。

但是实际上没有必要这样做。如果您打算进行进一步处理,那么重要的是数据结构及其中的内容,而不是其默认表示在屏幕上的显示方式。