使用CSV:以正确的顺序读取和写入数据

时间:2016-03-28 17:58:15

标签: python csv

我有两个与Twitter数据相关的.csv个文件。一个包含推文文本,另一个包含这些推文的ID。具有ID的文件是从中抽取另一个文件中的推文的总体。我正在尝试编写一个脚本来读取文本,在另一个文件中搜索相应的ID,然后在较小的样本中编写一个新的.csv文件,其中包含推文的ID和文本。 / p>

这是我到目前为止所拥有的:

import csv

# creates empty dictionary in which to store tweetIDs and tweet text
originals_data = {}

# declares an empty list to hold tweet text from coded datafile
# will be used to compare against the dictionary created earlier
coded_data = []
coded_all = []  # for all, not just text

# list to hold the IDs belonging to coded tweets for the round
tweet_IDs_for_coded = []

with open('first20.csv', 'rt') as round_in, open('gg_originals.csv', 'rt') as original_in:

    # reader object for gg_originals
    readOrigin = csv.reader(original_in, delimiter=',')
    # adds values from .csv file into the dictionary
    for row in readOrigin:
        originals_data[row[0]] = row[1]

    # reader object for round_x data
    readRound = csv.reader(round_in, delimiter=",")
    # appends the tweet text to a list
    for row in readRound:
        coded_data.append(row[0])

    # iterates over id:text dictionary
    for tweet_id in originals_data:
        # iterates over coded_data
        for tweet in coded_data:
            # When tweet in list matches text in dict, sends key to list
            if tweet == originals_data[tweet_id]:
                tweet_IDs_for_coded.append(tweet_id)

with open('first20.csv', 'rt') as round_in, open('test2.csv', 'wt') as output:
    # reader object for round_x data
    readRound = csv.reader(round_in, delimiter=",")
    # creates writer object to write new csv file with IDs
    writeNew = csv.writer(output, delimiter=",")
    # list that holds everything that's going into the csv file
    everything = []
    # sets row to equal a single row from round data
    row = next(readRound)
    row.insert(0, 'ID')
    # appends ID and then all existing data to list of rows
    everything.append(row)
    for i, row in enumerate(readRound):
        everything.append([str(tweet_IDs_for_coded[i])] + row)
    writeNew.writerows(everything)

填充文件(gg_originals.csv)的数据如下所示:

tweet_id_str,text
534974890168700930,abcd
534267820071084033,abce
539572102441877504,abcf
539973576108294145,abcg
529278820876943361,abch
529583601244176384,abci
535172191743397888,abcj
532195210059874304,abck
537812033895669760,abcl
,
,

作为人口子集的纯文本文件如下所示:

text
abcl
abci
abcd

到目前为止我运行的是,它似乎获得了正确的ID,甚至将它们写入新.csv文件中的新列。但是,新文件中的ID不在正确的行中 - 它们显示在实际上不对应的文本行中,这很糟糕!

新文件应该看起来像这样:

ID,text
537812033895669760,abcl
529583601244176384,abci
534974890168700930,abcd

相反,它最终会像这样:

ID,text
529583601244176384,abcl
537812033895669760,abci
534974890168700930,abcd

找到了正确的ID,但它们已被写入错误的行。

1 个答案:

答案 0 :(得分:1)

好的,这段代码确实(我认为)你想做什么。我问你的操作系统的原因是wt会在Windows中提供双倍行距的csv,所以我不得不使用wb。此外,在单元格A1中插入大写“ID”会导致使用Excel打开时出现类型问题。一切都很有趣:)

我最终没时间跟踪你的错误并仍然给出了答案,所以我写了答案,如果我有机会并且突出你的工作不同步的地方我会回去(我是从来没有遇到过Excel中的SYLK错误,所以分心!)。

我交换了你的字典。推文本身成为了这个词的关键。不再迭代字典了。这也意味着您只需要打开first20.csv一次。你原来的方法有点复杂。

import csv

with open('gg_originals.csv', 'rt') as original_in:
    readOrigin = csv.reader(original_in, delimiter = ',')
    originals_data = {row[1]: row[0] for row in readOrigin}

with open('first20.csv', 'rt') as round_in:
    input_data = csv.reader(round_in)
    data_to_match = [row[0] for row in input_data]

compiled_list = []
for item in data_to_match:
    compiled_list.append([item, originals_data[item]])

with open('testoutput.csv', 'wt') as outfile:
    writer = csv.writer(outfile)
    writer.writerows(compiled_list)