Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我需要一些帮助来写入excel文件_Python_Excel_Csv - Fatal编程技术网

Python 我需要一些帮助来写入excel文件

Python 我需要一些帮助来写入excel文件,python,excel,csv,Python,Excel,Csv,我正在编写一个任务,其中用户将输入一个句子(无标点符号),程序将把单词存储在一个列表中,然后用创建的列表中单词的位置替换每个单词。 我对如何处理这个问题没有太多的想法,因为我是python新手,但是我假设我应该首先将单词存储在Excel数据库中,然后将它们放在列表中。 如果我能在这方面得到一些帮助,我将不胜感激,谢谢 有关此任务的更多信息,请查看此屏幕截图:下面是一个如何执行此任务的快速示例。记住做你自己的工作,只用于学习 sentence = input("Write a sentence:

我正在编写一个任务,其中用户将输入一个句子(无标点符号),程序将把单词存储在一个列表中,然后用创建的列表中单词的位置替换每个单词。 我对如何处理这个问题没有太多的想法,因为我是python新手,但是我假设我应该首先将单词存储在Excel数据库中,然后将它们放在列表中。 如果我能在这方面得到一些帮助,我将不胜感激,谢谢


有关此任务的更多信息,请查看此屏幕截图:

下面是一个如何执行此任务的快速示例。记住做你自己的工作,只用于学习

sentence = input("Write a sentence: ")

sentence_array = sentence.lower().split(" ") # Split lowercased sentence to an array
individuals = [] # List for individual words

for x in range(0, len(sentence_array)): # Loop to find individual words
    if sentence_array[x] not in individuals: # If word is not picked already...
        individuals.append(sentence_array[x]) # ...append it to individuals list

positions = [] # List that contains positions of the words
for x in range(0, len(sentence_array)): # Loop to append positions to a list
    positions.append(individuals.index(sentence_array[x]) + 1) # Appending indexes of words, notice +1

with open("Result file.txt", "w") as f: # Write results to a file
    for x in range(0, len(positions)): # Loop to write indexes
        if x == len(positions)-1: # If last position, write line break instead of comma
            f.write("%s\n" % positions[x])
        else:
            f.write("%s," % positions[x])
    for y in range(0, len(individuals)): # Loop to write words
        if y == len(individuals)-1: # If last word, write line break instead of comma
            f.write("%s\n" % individuals[y])
        else:
            f.write("%s, " % individuals[y])

可以在Result file.txt中查看输出。您应该付出一些努力,并就代码提出更具体的问题。 下面是一个使用numpy阵列的简短解决方案:

#ask for sentence, split the words by space and put the words in an array
sentence = np.array(raw_input("Enter a sentence: ").split(' '))

#find unique words and save their indices in the array/sentence (np.unique does not keep the order)
unique_words, index = np.unique(sentence, return_index = True)

#get unique words in sorted order by sorting the indices
unique_sorted = sentence[np.sort(index)]

#loop through sentence and return indices of the words in sorted unique words
result = [np.where(unique_sorted == word)[0][0] for word in sentence]
请注意,输出将移位1,因为python索引从0开始:

>>> sentence = np.array(raw_input("Enter a sentence: ").split(' '))
Enter a sentence: ask not what your country can do for you ask what you can do for your country

>>> result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 2, 8, 5, 6, 7, 3, 4]
也许它对你使用数组和其他东西来说太复杂了,这就是为什么你应该尝试不同的列表。但是你得到了基本的想法:

  • 获取您的输入,拆分单词

  • 按排序顺序查找唯一的单词

  • 对于句子中的每个单词,在排序数据中查找索引


  • 这根本不需要Excel。你应该尝试一些东西,当你有更具体的问题时再来。@M4rtini谢谢你的评论,你建议我怎么做?我可以把它们储存在基本列表中吗?谢谢你的回复。这确实帮助我理解了我的问题,我很高兴能帮助你。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。