【问题标题】:merging and removing duplicates in two csv's without using pandas在不使用熊猫的情况下合并和删除两个csv中的重复项
【发布时间】:2018-09-13 14:28:26
【问题描述】:

我有一个如下所示的测试输出 csv;

test1   test success
test2   test failed
regtest failed to build

第 1 列包含唯一字符串,第 2 列包含以下三个字符串之一;测试成功,测试失败,构建失败。

我经常在新版本上运行此测试,我想将最新测试的 csv 与之前的测试进行比较。 我想生成一个新的 csv,其中包含状态(第 2 列)已更改的所有测试。最好采用以下格式;

TestName OldState NewState

这是我目前的尝试,它得到了两个文件之间的所有差异,但看起来像这样;

test1   test success
test2   test failed
regtest failed to build
test2   test success

我需要一种方法将第二个 test2 与第一个合并,所以它看起来像这样;

test1   test success
test2   test failed      test success
regtest failed to build

我当前的代码;

import csv
import sys

with open(sys.argv[1], 'r') as t1, open(sys.argv[2], 'r') as t2, open(sys.argv[2], 'r') as t3, open(sys.argv[1], 'r') as t4:
    fileOne = t1.readlines()
    fileTwo = t2.readlines()
    fileThree = t3.readlines()
    fileFour = t4.readlines()

with open(sys.argv[3], 'w') as outFile:
    for line in fileTwo:
        if line not in fileOne:
            outFile.write("From File 2," + line)

    for line in fileFour:
        if line not in fileThree:
            outFile.write("\r\nFrom File 1," + line)

【问题讨论】:

  • 编辑我使用python 3.3

标签: python csv


【解决方案1】:

csv 中的每一行都有一个键值对,因此将 csv 写入字典是有意义的,然后您可以轻松地根据键比较两个值

with open(sys.argv[1], 'r') as t1, open(sys.argv[2], 'r') as t2:
    # Convert lines of Csv to list
    reader1 = list(csv.reader(t1))
    reader2 = list(csv.reader(t2))

# Create a dictionary of key value pairs for t1
fileOne_dict = {col[0]: [col[1]] for col in reader1}

# compare values based on keys and append if different
for col in reader2:
    if fileOne_dict.get(col[0]):
        if fileOne_dict[col[0]][0] != col[1]:
            fileOne_dict[col[0]].append(col[1])
    else:
        fileOne_dict[col[0]] = ["", col[1]]

out = [[key]+value for key, value in fileOne_dict.items()]
print(out)

with open ('diff.csv', 'w') as outFile:
    writer = csv.writer(outFile)
    writer.writerows(out)

这应该会给你留下一本包含你想要的信息的字典

【讨论】:

  • 当我使用此代码时,我得到字符串 {"T":"e R R R e e e e e e e...,"P":"B"}?
  • 抱歉,我以为您会通过读取 csv 而不是字符串来获得值列表。我会附加代码
  • 我得到:KeyError: 'T' from line 9
  • 尝试再次复制代码,您可能错过了编辑,我觉得还不错,如果还是不行,请发布您的 csv
  • 你说得对,我漏掉了一些东西。但是给你一个新的错误:)如果第二个文件中出现了一个新的测试,而第一个文件中没有
猜你喜欢
  • 2023-02-06
  • 2019-04-03
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 2021-04-27
  • 2015-01-08
  • 2020-06-11
相关资源
最近更新 更多