2016-03-07 83 views
1

我寫了一個for循環,通過CSV迭代得到像這樣的列表:寫作從一個for循環列表到CSV

[t1, s1] 
[t2, s2] 
[t3, s3] 

,並做4萬次。 現在我需要將它們寫入一個新的CSV文件,它們將填充2個字段並用逗號分隔。 當我輸入這個時,我只能得到最後一個循環的最後一個列表,並且在單元格中有一個字符。

def sentiment_analysis(): 
    fo = open("positive_words.txt", "r") 
    positive_words = fo.readlines() 
    fo.close() 
    positive_words = map(lambda positive_words: positive_words.strip(), positive_words) 
    fo = open("negative_words.txt", "r") 
    negative_words = fo.readlines() 
    fo.close() 
    negative_words = map(lambda negative_words: negative_words.strip(), negative_words) 
    fo = open("BAC.csv", "r") 
    data = fo.readlines() 
    fo.close() 
    data = map(lambda data: data.strip(), data) 
    x1 = 0 #number of bullish 
    x2 = 0 #number of bearish 
    x3 = 0 #number of unknown 
    for info in data: 
     data_specs = info.split(',') 
     time_n_date = data_specs[0] 
     sentiment = data_specs[2] 
     '''Possibly precede with a nested for loop for data_specs???''' 
     if sentiment == 'Bullish': 
      '''fo.write(time + ',' + 'Bullish' + '\n')''' 
     elif sentiment == 'Bearish': 
      ''' fo.write(time + ',' + 'Bearish' + '\n')''' 
     else: 
      x3 += 1 
      positive = 0 
      negative = 0 
      content_words = data_specs[1].split() 
      for a in positive_words: 
       for b in content_words: 
        if (a == b): 
         positive = positive + 1 
      for c in negative_words: 
       for d in content_words: 
        if (c == d): 
         negative = negative + 1 
      if positive > negative: 
       '''fo.write(time + ',' + 'Bullish' + '\n')''' 
       sentiment = 'Bullish' 
      elif positive < negative: 
       sentiment = 'Bearish' 
      else: 
       sentiment = 'Neutral' 
     bac2data = [time_n_date, sentiment] 
     print bac2data 
     fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w") 
     for x in bac2data: 
      w = csv.writer(fo, delimiter = ',') 
      w.writerows(x) 
     fo.close() 

我的for循環沒有經歷這一切。

+0

我們可以看到你的代碼用來讀取數據? – Ittociwam

+0

@Ittociwam完成。 – squidvision

+2

以追加模式打開,你在每次迭代中覆蓋你的文件 – njzk2

回答

2

在您的代碼中bac2data = [time_n_date, sentiment]創建一個包含2個字符串項目的列表。將它寫入csv.writer()的CSV文件的正確方法是使用writerow(bac2data)

代碼的最後一部分包含許多錯誤。首先,您將以寫入模式('w')爲輸入數據的每一行打開CSV文件。這將每次覆蓋文件,丟失除最後一行以外的所有數據。然後,您正在迭代bac2data列表並在每個項目上調用writerows()。這將在它自己的行上寫入字符串中的每個字符(與您報告的輸出相匹配)。

相反,打開輸出文件並創建一個csv.writer主要for info in data:循環之外:

bac2data = [time_n_date, sentiment] 
    print bac2data 
    fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w") 
    for x in bac2data: 
     w = csv.writer(fo, delimiter = ',') 
     w.writerows(x) 
    fo.close() 

與此:

fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w") 
writer = csv.writer(fo) 
for info in data: 
    .... 

然後在主循環的底部更換這些線路:

bac2data = [time_n_date, sentiment] 
    print bac2data 
    writer.writerow(bac2data) 

一旦你有這個工作,並沒有升onger需要打印bac2data進行調試,您可以只使用1線:

writer.writerow((time_n_date, sentiment)] 

更新

爲功能完整代碼:

def sentiment_analysis(): 
    fo = open("positive_words.txt", "r") 
    positive_words = fo.readlines() 
    fo.close() 
    positive_words = map(lambda positive_words: positive_words.strip(), positive_words) 
    fo = open("negative_words.txt", "r") 
    negative_words = fo.readlines() 
    fo.close() 
    negative_words = map(lambda negative_words: negative_words.strip(), negative_words) 
    fo = open("BAC.csv", "r") 
    data = fo.readlines() 
    fo.close() 
    data = map(lambda data: data.strip(), data) 
    x1 = 0 #number of bullish 
    x2 = 0 #number of bearish 
    x3 = 0 #number of unknown 

    fo = open("C:\Users\Siddhartha\Documents\INFS 772\Project\Answer\BAC2_answer.csv", "w") 
    writer = csv.writer(fo) 

    for info in data: 
     data_specs = info.split(',') 
     time_n_date = data_specs[0] 
     sentiment = data_specs[2] 
     '''Possibly precede with a nested for loop for data_specs???''' 
     if sentiment == 'Bullish': 
      '''fo.write(time + ',' + 'Bullish' + '\n')''' 
     elif sentiment == 'Bearish': 
      ''' fo.write(time + ',' + 'Bearish' + '\n')''' 
     else: 
      x3 += 1 
      positive = 0 
      negative = 0 
      content_words = data_specs[1].split() 
      for a in positive_words: 
       for b in content_words: 
        if (a == b): 
         positive = positive + 1 
      for c in negative_words: 
       for d in content_words: 
        if (c == d): 
         negative = negative + 1 
      if positive > negative: 
       '''fo.write(time + ',' + 'Bullish' + '\n')''' 
       sentiment = 'Bullish' 
      elif positive < negative: 
       sentiment = 'Bearish' 
      else: 
       sentiment = 'Neutral' 

     bac2data = [time_n_date, sentiment] 
     print bac2data 
     writer.writerow(bac2data) 

    fo.close() 
+0

所以我有這個。 進口模塊 高清sentiment_analysis(): FO =打開( 「文件路徑」, 「W」) W = csv.writer(FO,分隔符= '') 在數據信息: ... bac2data = time_n_date,情緒 w.writerow(bac2data) fo。關閉() 我現在只在一行上得到上次迭代的列表。 謝謝! – squidvision

+0

@squidvision:你的數據是什麼?有超過1行嗎?我無法從評論中的代碼中看出縮進的級別。我已根據您發佈的代碼更新了完整解決方案的答案。 – mhawke