我有一個有兩列一個用於分享Tweet,另一個用於情感值格式化.CSV文件像這樣(但幾千年的鳴叫):將CSV文件的行轉換爲元組列表?
I like stackoverflow,Positive
Thanks for your answers,Positive
I hate sugar,Negative
I do not like that movie,Negative
stackoverflow is a question and answer site,Neutral
Python is oop high-level programming language,Neutral
我想獲得的輸出是這樣的:
negfeats = [('I do not like that movie','Negative'),('I hate sugar','Negative')]
posfeats = [('I like stackoverflow','Positive'),('Thanks for your answers','Positive')]
neufeats = [('stackoverflow is a question and answer site','Neutral'),('Python is oop high-level programming language','Neutral')]
我已經嘗試過下面這樣做,但我在元組中找到了一些缺失的字符。另外,如何將x,y和z保留爲整數而不是浮點數?
import csv
neg = ['Negative']
pos = ['Positive']
neu = ['Neutral']
neg_counter=0
pos_counter=0
neu_counter=0
negfeats = []
posfeats = []
neufeats = []
with open('ff_tweets.csv', 'Ur') as f:
for k in f:
if any(word in k for word in neg):
negfeats = list(tuple(rec) for rec in csv.reader(f, delimiter=','))
neg_counter+=1
elif any(word in k for word in pos):
posfeats = list(tuple(rec) for rec in csv.reader(f, delimiter=','))
pos_counter+=1
else:
neufeats = list(tuple(rec) for rec in csv.reader(f, delimiter=','))
neu_counter+=1
x = neg_counter * 3/4
y = pos_counter * 3/4
z = neu_counte * 3/4
print negfeats
print posfeats
print neufeats
print x
print y
print z
你應該要小心 - 在Python 2.7(如題)'3/4'回報'0'。 – will
@ will true,讓我們解決這個問題。 –
@ÉbeIsaac這個解決方案理論上應該可以工作,但是當我運行它時,我得到了空列表,用於negfeats,posfeats和neufeats – Alsphere