比较 2 个 csv 文件并检查前 2 列,如果匹配,请用户决定是否覆盖,然后继续下一行

发布于 2025-01-11 13:59:12 字数 1485 浏览 0 评论 0原文

我有一个用例,其中有 2 个 CSV 文件,每个 CSV 文件中有一些行,每个文件都有三列。比较 2 个 csv 文件的前 2 列,如果匹配,则询问用户输入是否要使用第二个 csv 文件中的值覆盖第一个 csv 文件中的行,如果不中止操作。

当我第一次运行 python 代码时,它应该使用从第二个 CSV 文件到第一个 csv 文件的新值更新 csv 文件,但是对于连续运行我的 python 代码,我必须检查前 2 列是否匹配并要求用户决定他是否需要覆盖这些值,因为现在第一个 csv 文件将包含第一个 csv 文件中的行。

我的代码:

import csv
import sys

def csv_file_copy():
   csv_file = input("Enter the CSV file needs to be updated ")
   csv_file_cp = input("Enter the csv file from where the data needs to be copied ")
   csvfile = open(csv_file_cp, 'r',encoding="utf-8-sig")
   reader = csv.reader(csvfile)
   csv_file_orig = open(csv_file, 'r',encoding="utf-8-sig")
   reader2 = csv.reader(csv_file_orig)
   res = []
   for row in reader:
     print("This is row", row)
     for row2 in reader2:
       print("This is row2", row2)
       if (row2[0] == row[0] and row2[1] == row[1]):
         user_input = input("Store type and store number already exists in the csv file, continue? y/n ").lower()
         if user_input == "y":
           res.append(row)
         elif user_input == "n":
            print("Aborting operation")
            sys.exit(1)
       else:
        res.append(row2)
     res.append(row)
     continue
   print (reader)
   with open(csv_file, 'w') as csv_file1:
      writer = csv.writer(csv_file1, delimiter=',')
      for row in res:
        writer.writerow(row)

csv_file_copy()

当对相同的 2 个文件第二次执行代码时,第二个 for 循环仅运行一次,因此仅匹配一个值,但大约有 10 个匹配的值,这对我不起作用。

I have a use case where I have 2 CSV files with some rows in each CSV file, and they have three columns each. Compare the 2 csv files for first 2 columns and if it matches then ask the user input if he wants to override the row in the first csv file with the values from second csv file, if not abort the operation.

First time when I run the python code it should update the csv file with the new values from the 2nd CSV file to first csv file, but for consecutive runs of my python code I have to check if first 2 columns match and ask the user to decide if he needs to override the values or not, since now the first csv file will have rows from first csv file.

My code:

import csv
import sys

def csv_file_copy():
   csv_file = input("Enter the CSV file needs to be updated ")
   csv_file_cp = input("Enter the csv file from where the data needs to be copied ")
   csvfile = open(csv_file_cp, 'r',encoding="utf-8-sig")
   reader = csv.reader(csvfile)
   csv_file_orig = open(csv_file, 'r',encoding="utf-8-sig")
   reader2 = csv.reader(csv_file_orig)
   res = []
   for row in reader:
     print("This is row", row)
     for row2 in reader2:
       print("This is row2", row2)
       if (row2[0] == row[0] and row2[1] == row[1]):
         user_input = input("Store type and store number already exists in the csv file, continue? y/n ").lower()
         if user_input == "y":
           res.append(row)
         elif user_input == "n":
            print("Aborting operation")
            sys.exit(1)
       else:
        res.append(row2)
     res.append(row)
     continue
   print (reader)
   with open(csv_file, 'w') as csv_file1:
      writer = csv.writer(csv_file1, delimiter=',')
      for row in res:
        writer.writerow(row)

csv_file_copy()

When the code is executed second time against the same 2 files the second for loop runs only once thereby matching only one value but there are about 10 values that is matching which doesn't work for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三五鸿雁 2025-01-18 13:59:12

如果csv_file_orig不太大(或者您的可用内存太低),那么您可以将整个内容存储到列表中。

而不是

reader2 = csv.reader(csv_file_orig)

使用Afterwards。

csv_file_orig_lines = list(csv.reader(csv_file_orig))

您可以根据需要多次迭代 csv_file_orig_lines 列表,

If the csv_file_orig is not too big (or your available memory too low) then you may store the whole contents into a list.

Instead of

reader2 = csv.reader(csv_file_orig)

You'll use

csv_file_orig_lines = list(csv.reader(csv_file_orig))

Afterwards you may iterate through csv_file_orig_lines list as many times as you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文