读取两个 csv 文件并比较每一行。如果行匹配打印两行,如果不相似则打印无效
Read two csv files and compare every line. If the lines match print both lines, if it isn't similar print invalid
import csv
f1 = open("file1.csv")
f2 = open("file2.csv")
csv_f1 = csv.reader(f1)
csv_f2 = csv.reader(f2)
for row1 in csv_f1:
for row2 in csv_f2:
if row1 == row2:
print row1[0], row2[0]
else:
print row1[0], "Invalid"
此程序未打印出第 1 行、第 2 行。它只是打印 file1 的第一行并且在同一行多次无效。
您需要比较相应的行,而不是每一行与每一行,这是您的代码现在所做的。
from __future__ import print_function
import csv
import itertools
import sys
# Select the right function based on whether we are in Python 2 or 3.
if sys.version_info.major == 2:
zip_longest = itertools.izip_longest
else:
zip_longest = itertools.zip_longest
f1 = open("file1.csv")
f2 = open("file2.csv")
csv_f1 = csv.reader(f1)
csv_f2 = csv.reader(f2)
for row1, row2 in zip_longest(csv_f1, csv_f2):
if row1 == row2:
print(row1[0], row2[0])
else:
print(row1[0], "Invalid")
- 读取两个 CSV 文件并创建以行号为键、以行为值的字典
- file1 上的迭代器,即
root1
并与 file2 进行比较,即 root2
具有相同的键。
- 当 file1 的条目多于 file2 时处理异常。
代码:
import csv
with open("/home/infogrid/Desktop/file1.csv", "rb") as fp1:
root = csv.reader(fp1,)
root1 = {}
for i in root:
root1[root.line_num] = i
with open("/home/infogrid/Desktop/file2.csv", "rb") as fp1:
root = csv.reader(fp1,)
root2 = {}
for i in root:
root2[root.line_num] = i
for i in root1:
try:
if root1[i]==root2[i]:
print root1[i], root1[i]
else:
print root1[i], "Invalid"
except:
print root1[i], "Invalid"
输出:
['test1', 'test3', 'test4'] ['test1', 'test3', 'test4']
['test1', 'test5', 'test4'] Invalid
['test1', 'test3', 'test4'] ['test1', 'test3', 'test4']
['test1', 'test3', 'test4'] Invalid
import csv
f1 = open("file1.csv")
f2 = open("file2.csv")
csv_f1 = csv.reader(f1)
csv_f2 = csv.reader(f2)
for row1 in csv_f1:
for row2 in csv_f2:
if row1 == row2:
print row1[0], row2[0]
else:
print row1[0], "Invalid"
此程序未打印出第 1 行、第 2 行。它只是打印 file1 的第一行并且在同一行多次无效。
您需要比较相应的行,而不是每一行与每一行,这是您的代码现在所做的。
from __future__ import print_function
import csv
import itertools
import sys
# Select the right function based on whether we are in Python 2 or 3.
if sys.version_info.major == 2:
zip_longest = itertools.izip_longest
else:
zip_longest = itertools.zip_longest
f1 = open("file1.csv")
f2 = open("file2.csv")
csv_f1 = csv.reader(f1)
csv_f2 = csv.reader(f2)
for row1, row2 in zip_longest(csv_f1, csv_f2):
if row1 == row2:
print(row1[0], row2[0])
else:
print(row1[0], "Invalid")
- 读取两个 CSV 文件并创建以行号为键、以行为值的字典
- file1 上的迭代器,即
root1
并与 file2 进行比较,即root2
具有相同的键。 - 当 file1 的条目多于 file2 时处理异常。
代码:
import csv
with open("/home/infogrid/Desktop/file1.csv", "rb") as fp1:
root = csv.reader(fp1,)
root1 = {}
for i in root:
root1[root.line_num] = i
with open("/home/infogrid/Desktop/file2.csv", "rb") as fp1:
root = csv.reader(fp1,)
root2 = {}
for i in root:
root2[root.line_num] = i
for i in root1:
try:
if root1[i]==root2[i]:
print root1[i], root1[i]
else:
print root1[i], "Invalid"
except:
print root1[i], "Invalid"
输出:
['test1', 'test3', 'test4'] ['test1', 'test3', 'test4']
['test1', 'test5', 'test4'] Invalid
['test1', 'test3', 'test4'] ['test1', 'test3', 'test4']
['test1', 'test3', 'test4'] Invalid