2016-07-23 72 views
0

句子池匹配一個單詞我有兩個不同的文件文件「句子」中包含的句子池,請看下面的快照。 Sentence Snapshot要使用Python

文件「Word」contians詞彙池,請在下面找到快照。

Word file snap shot

我要地圖從Word文件中的單詞到句子的文件,如果任何詞匹配的那句話,我想例如導致句子的形式和匹配字

: 句子匹配詞 Linux和開放堆棧是偉大的Linux開放堆棧

請在下面找到我的代碼,當我試圖提取結果到csv,其顯示錯誤。

import pandas as pd 
import csv 

sentence_xlsx = pd.ExcelFile('C:\Python\Seema\Sentence.xlsx') 
sentence_all = sentence_xlsx.parse('Sheet1') 
#print(sentence_all) 
word_xlsx = pd.ExcelFile('C:\Python\Seema\Word.xlsx') 
word_all = word_xlsx.parse('Sheet1')    


for sentence in sentence_all['Article']: 
    sentences = sentence.lower() 

    for word in sentences.split(): 
     if word in ('linux','openstack'): 
      result = word,sentence 

results = open('C:\Python\Seema\result.csv', 'wb') 
writer = csv.writer(results, dialect='excel') 
writer.writerows(result) 
results.close() 

Traceback (most recent call last): 
    File "Word_Finder2.py", line 25, in <module> 
    results = open('C:\Python\Seema\result.csv', 'wb') 
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\Python\\Seema\result.c 
sv' 
+0

請添加錯誤 –

+0

@Moses Koledoye的完整回溯:我已經添加了追蹤錯誤 –

+0

嘗試打開與''w''模式,而不是。您仍可以得到另一個錯誤,因爲'writerows'與行的列表的作品,而不是任何可迭代 –

回答

1

您的路徑的'\result.csv'部分都有其'\r'讀爲一個回車符。要解決此問題,請將前導r附加到路徑中,使其成爲原始字符串文字(credit @georg)。


然後用writerows,從所有迭代的結果應該被累積成一個列表,而不僅僅是最後的結果。

result = [] # create list to hold result from each iteration 
for sentence in sentence_all['Article']: 
    sentences = sentence.lower() 

    for word in sentences.split(): 
     if word in ('linux','openstack'): 
      # append result of iteration to result 
      result.append([sentence, word]) 
      #|<- creates list of rows suitable for 'writerows' 

results = open(r'C:\Python\Seema\result.csv', 'wb') 
#   ^prevents \r... from being read as a special character 
writer = csv.writer(results, dialect='excel') 
writer.writerows(result) 
results.close() 
+0

摩西我做到了這一點,但仍然沒有得到理想的結果。 –

+0

期望的結果是什麼? –

+0

我想在一列中的句子,並在第二列 –

相關問題