将for循环中的列表写入csv

时间:2016-03-07 01:05:42

标签: python for-loop export-to-csv sentiment-analysis

我写了一个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循环并没有全部通过。

1 个答案:

答案 0 :(得分:2)

在您的代码中System.out.println("Do you want to use a lifeline now? (y - yes or n - no)"); answer =myScanner.nextLine(); boolean life=false; if (answer.equals("y")) { do { System.out.println("Alright. Which one of your lifelines would you like to use?( f-fifty-fifty , a-ask the audience, c-call a friend)"); life_1= myScanner.nextLine(); if (life_1.equals("f") && lifeline_5050 !=0){ System.out.println(" The answer is not b or d"); lifeline_5050 = lifeline_5050 - 1; life=true; } else if (life_1.equals("a") && lifeline_ask != 0) { System.out.println("The audeince say the answers are: a."+randomNum1+ "b. "+randomNum2+"c. "+randomNum3+ "d. "+randomNum4 ); lifeline_ask= lifeline_ask -1 ; life=true; } else if (life_1.equals("c") && lifeline_call !=0 ){ System.out.println("Your friend says the answer a is "+RandomNum+" possible."); lifeline_call = lifeline_call -1; life=true;} else { System.out.println("Invalid answer. You can't use this lifeline."); } } while (life==false); } 创建一个包含2个字符串项的列表。将其写入bac2data = [time_n_date, sentiment]的CSV文件的正确方法是使用csv.writer()

代码的最后一部分包含许多错误。首先,您要以写入模式(writerow(bac2data))为输入数据的每一行打开CSV文件。这将每次覆盖文件,丢失除最后一行之外的所有数据。然后,您将遍历'w'列表并在每个项目上调用bac2data。那将是从它自己的行上的字符串中写出每个字符(与你报告的输出相匹配)。

相反,打开输出文件并在主writerows()循环之外创建csv.writer

for info in data:

然后在主循环的底部替换这些行:

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
    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()

一旦你有了工作,不再需要打印 bac2data = [time_n_date, sentiment] print bac2data writer.writerow(bac2data) 进行调试,你可以使用1行:

bac2data

<强>更新

完整的功能代码:

    writer.writerow((time_n_date, sentiment)]