2015-04-14 52 views
-2

我打開文件夾,然後試圖標記CSV文件中的每個單詞。這段代碼是否正確?我試圖讀取文件,然後標記,但我看不到結果。我是編程新手,有人可以幫我嗎?如何在Python中標記我的CSV文件?

filename=open("positivecsv.csv","r") 
type(raw) #str 
tokens = [] 
for line in filename.readlines(): 
    tokens+=nltk.word_tokenize(line) 

>>> print tokens 
+1

Python已經有一個CSV API,所以你不必擔心標記文件。請參閱Python手冊的[第13.1節](https://docs.python.org/2/library/csv.html)。 – Wernsey

回答

1

Python有一個內置的CVS readerwriter ,所以你需要自己做。

下面是一個例子:

import csv 

with open('positivecsv.csv', 'r') as csvfile: # this will close the file automatically. 
    reader = csv.reader(csvfile) 
    for row in reader: 
     print row 

行將被包含當前行的所有單元的明細表。

相關問題