csv文件比较,解析和写作(线性?)

时间:2016-08-10 18:16:18

标签: python python-3.x csv parsing

以下代码查看csv文件,file1和file2。根据每个文件中的唯一键,将它们与csv交叉引用文件进行比较,如果匹配,则将file1和file2中的唯一标识符进行比较,如果匹配则将从file2中选择值并执行写入,否则它将从file1中选择值并进行写入。

import csv, datetime, calendar
global_dic = {}
with open('D:\\hello.csv', 'r') as file0:

    reader1 = csv.reader(file0, delimiter='\t')
    header = next(reader1)

    for row in reader1:
        key = (row[0] + '|' + row[1] + '|' + row[2] + '|' + row[3])
        global_dic[key] = {header[0]: row[0], header[1]: row[1], header[2]: row[2], header[3]: row[3], header[4]: row[4], header[5]: row[5], header[6]: row[6], header[7]: row[7], header[8]: row[8], header[9]: row[9]}


with open('D:\\file1.csv', 'r') as master:
    master_indices = dict(((r[0] + r[1] + r[2] + r[3] + r[4]), i) for i, r in enumerate(csv.reader(master)) if '/' in r[0])

with open('D:\\file2.csv', 'r') as hosts:
    with open('results1.csv', 'w') as results:
        reader = csv.reader(hosts)
        writer = csv.writer(results)

        writer.writerow(next(reader, []) + ['RESULTS'])

        for row in reader:

            if '/' in row[0]:
                key = row[1] + row[2] + row[3] + row[4]
                value = row[6]
                if key in global_dic:

                    meow = datetime.datetime.strptime(row[0], '%m/%d/%Y %H:%M:%S')  # change str time to date/time obj && add .%f for ms consolidation
                    unix_timestamp = calendar.timegm(meow.timetuple())  # do the conversion to unix stamp
                    time_ms = unix_timestamp * 1000

                    index = master_indices.get(row[0] + row[1] + row[2] + row[3] + row[4])
                    if index is not None:
                        a = ''
                    else:
                        writer.writerow('value' + ',' + str(global_dic[key]['cpKey']) + ',' + str(global_dic[key]['SCADA Key']) + ',' + str(value) + ',' + str(time_ms) + ',' + str(time_ms) + ',' + '0' + ',' + '0' + ',' + '0' + '\n')

问题需要帮助: 当没有匹配时我可以写文件,但是当有匹配时我不知道如何从master_indecies带来r [6]。如果我在字典中包含r [6]而不是在匹配过程中创建问题

                    if index is not None:
                        # HELPPPPPPPPPP
                    else:
                        writer.writerow('value' + ',' + str(global_dic[key]['cpKey']) + ',' + str(global_dic[key]['SCADA Key']) + ',' + str(value) + ',' + str(time_ms) + ',' + str(time_ms) + ',' + '0' + ',' + '0' + ',' + '0' + '\n')

1 个答案:

答案 0 :(得分:0)

首先提供的代码太复杂了,试着让它变得更容易。使用更好的名称(哪个文件包含这些交叉引用?)

我认为较小的步骤是解决这个问题的正确方法,有些想法如下:

dict1 = get_dict_from_csv('file1.csv')
dict2 = get_dict_from_csv('file2.csv')
cross_refs = get_cross_refs_from_csv('cross_refs.csv')
with open('result.csv', mode='w') as result:
    for row in dict1:
        #...
        if index is not None:
            writer.writerow(dict2[index])

将使您的生活更轻松,所以我的第二个建议是,重构您的代码第一。无论如何祝你好运!

[edit]请看一下:Read two textfile line by line simultaneously -python以获取一些如何在python中同时读取多个文件的示例。

干杯拉尔斯