如何分析句子,然后列出csv文件中的位置

时间:2016-03-10 13:04:09

标签: python csv

我正在尝试编写一个脚本来识别句子中的单个单词,将这些单词存储在一个列表中,并将原始句子中的每个单词替换为该单词在列表中的位置。

例如,句子:

ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY

我想输出代码:

1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5

这是我到目前为止所尝试的:

import csv
file = open("file.csv", "a+")
sentence =  ["I", "LIKE", "TO", "GO", "TO", "THE", "PARK"]
found = ""
for position, sentence in enumerate(sentence):
      found = found + str(position +1) + ","
new_record = found+","+sentence
file.write(str(found))
file.close()

仅输出1,2,3,4,5,6,7,8..

3 个答案:

答案 0 :(得分:2)

尝试这样的事情:

sentence =  ["MY", "NAME", "IS", "JEFF", "AND", "JEFF", "LIKES", "CHEESE"]
found = ""
for word in sentence:
      found += str(sentence.index(word)+1) + ","

输出:1,2,3,4,5,4,7,8,

最后,应该使用found.rstrip(",")

来缩小

答案 1 :(得分:1)

您可以使用csv库执行此操作,如下所示:

import csv

text = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
words = text.split()

with open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow([words.index(word)+1 for word in words])

这会给你output.csv包含以下内容:

1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5

使用with可确保文件最后自动关闭,因此无需添加f_output.close()

答案 2 :(得分:0)

>>> the_list='ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'.split(" ")
>>> the_list
['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU', 'ASK', 'WHAT', 'YOU', 'CAN', 'DO', 'FOR', 'YOUR', 'COUNTRY']
>>> [the_list.index(item)+1 for index,item in enumerate(the_list)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5]

或者作为一个带字符串的函数(假设只有单词和空格的格式相同)

def indexer(the_string):
    return [the_string.split(" ").index(item)+1 for index,item in enumerate(the_string.split(" "))]

如果你想要一个只有1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5的字符串输出(仍然采用相同的输入格式),你可以只做一个该列表的字符串表示,并将[]替换为空字符串。

>>>str([the_string.split(" ").index(item)+1 for index,item in enumerate(the_string.split(" "))]).replace("]","").replace("[","")
'1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 9, 6, 7, 8, 4, 5'