我有两个具有相同结构的 csv 文件。
文件1.csv:
352075|6505286781|6505286781|165|A|2.6.37-3.2|25353gb1FAa8
172238|8136090512|8136090512|1|A|2.6.37-3.2|255411429E02
105767|1783707658|1783707658|82|A|2.6.37-3.2|375g1043DC92
352092|2214612065|2214612065|22|A|2.6.37-3.2|07k9975cad1e
文件2.csv:
274451|24575|24575|3872531727|23|24575|2017-11-08 11:43:21.15|2017-11-25 16:30:21.061|STB|375g1043DC92|375g1043DC92
506406|280335|280335|4516157218|22|280335|2017-11-22 15:44:54.307|2017-11-29 11:26:02.123|STB|256d9739d3cc|256d9739d3cc
367536|163226|163226|5007632889|9|163226|2017-11-15 20:37:02.034|2017-11-28 20:55:24.891|STB|25353gb1FAa8|25353gb1FAa8
374253|254874|254874|9263432532|23|254874|2017-11-16 19:17:52.827|2017-11-28 19:25:23.805|STB|37fe9739b5a0|37fe9739b5a0
我需要在文件2中检查文件1中的数据(文件1中的数据来自列[6])。
我想使用 Python 来做到这一点。我尝试过:
import csv
with open('file1.csv', newline='') as csvfile:
list1 = csv.reader(csvfile, delimiter='|')
for row in list1:
print(row[6])
但是我如何才能逐个检查这些数据file2.csv
,如果数据存在则打印它,如果不存在则提供另一个操作?
答案1
对于那些发现这个问题并喜欢使用 bash 解决方案的人来说,以下脚本用更少的行提供了相同的功能。
虽然可能存在一种计算效率更高的方法。这是我快速获取输出的方法,无需运行任何命令两次。如果不想要grep "$i" "$DRC"
输出。将作为测试。grep "$i" "$DRC"
if grep -q "$i" "$DRC"
#!/bin/bash
SRC=/path/to/file1.csv
DRC=/path/to/file2.csv
for i in $(cut -d "|" -f 7 "$SRC")
do
LINE="$(grep "$i" "$DRC")"
if [ $? == 0 ]
then
echo "$i Found in $LINE"
else
echo "$i NOT Found"
fi
done
答案2
您甚至不需要使用csv.reader()
它来执行此操作。下面是一个例子(在 Python 3.6 中运行),说明如何在不使用库的情况下执行此操作,只需使用 Python 内置函数和sys
用于解析命令行的库即可。下面是第一个没有csv
库的示例,我们称之为search_basic.py
:
#!/usr/bin/env python3.6
from sys import argv
def parse_files(name_1, name_2):
"""Opens two files and checks if pos #6 in each row of file 1 is present in file2 via simple build-ins"""
try:
with open(file=name_1) as file_1, open(file=name_2) as file_2:
data_1 = file_1.readlines()
data_2 = file_2.readlines()
mapping = {
row.strip('\n').split('|')[-1]: row.strip('\n').split('|')
for row in data_2
}
for row in data_1:
last_column = row.strip('\n').split('|')[-1]
if last_column in mapping:
print(f'{last_column} found in {mapping[last_column]}')
else:
print(f'{last_column} not found, doing other operation')
except FileNotFoundError as error:
print('{}'.format(error))
exit(1)
else:
return
if __name__ == "__main__":
if len(argv) <= 1:
print('No parameters given...')
exit(1)
elif len(argv) == 2:
print('Only one file was given...')
exit(1)
else:
parse_files(argv[1], argv[2])
exit(0)
但是如果你坚持使用csv
这里的例子库(在python 3.6中工作)两个用来实现csv.reader()
,我们称它为search_csv.py
:
#!/usr/bin/env python3.6
import csv
from sys import argv
def parse_files(name_1, name_2):
"""Opens two files and checks if pos #6 in each row of file 1 is present in file2 via simple build-ins"""
try:
with open(file=name_1) as file_1, open(file=name_2) as file_2:
mapping = {
row[-1]: row
for row in csv.reader(file_2, delimiter='|')
}
for row in csv.reader(file_1, delimiter='|'):
last_column = row[-1]
if last_column in mapping:
print(f'{last_column} found in {mapping[last_column]}')
else:
print(f'{last_column} not found, doing other operation')
except FileNotFoundError as error:
print('{}'.format(error))
exit(1)
else:
return
if __name__ == "__main__":
if len(argv) <= 1:
print('No parameters given...')
exit(1)
elif len(argv) == 2:
print('Only one file was given...')
exit(1)
else:
parse_files(argv[1], argv[2])
exit(0)
当然,您需要对两个文件都进行 chmod 以允许执行:
chmod 755 search_basic.py
chmod 755 search_csv.py
鉴于您的上述file1
和,两者都使用以下行(或相应的)file2
产生相同的输出:./search_basic.py file1 file2
./search_csv.py file1 file2
25353gb1FAa8 found in ['367536', '163226', '163226', '5007632889', '9', '163226', '2017-11-15 20:37:02.034', '2017-11-28 20:55:24.891', 'STB', '25353gb1FAa8', '25353gb1FAa8']
255411429E02 not found, doing other operation
375g1043DC92 found in ['274451', '24575', '24575', '3872531727', '23', '24575', '2017-11-08 11:43:21.15', '2017-11-25 16:30:21.061', 'STB', '375g1043DC92', '375g1043DC92']
07k9975cad1e not found, doing other operation