我需要将.accdb
文件与.csv
文件进行比较。
两个文件文件具有相同类型的记录,计数不匹配。因此,我希望显示那些不常见的记录,并将其存储到带有;
和列标题的新文本文件中。
.accdb :
ID Name
1 Mak
2 Smith
3 Jack
.csv文件:
ID Name
1 Mak
2 Smith
3 Jack
4 Johnson
5 Mike
预期的.txt文件:
ID; Name
4; Johnson
5; Mike
我的尝试:
import pyodbc
CSVfile = 'E:\Python\AccessCSVFiles\EMP.csv'
TEXTfile = 'E:\Python\AccessCSVFiles\EMP_UPDATES.txt'
conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:\Python\AccessCSVFiles\EMP.accdb;'
con = pyodbc.connect(conn_string)
cur = con.cursor()
SQLQuery = 'SELECT * FROM EMP;'
rows = cur.execute(SQLQuery).fetchall()
records = [tuple(map(str,record)) for record in rows]
accessfile = set(records)
with open(CSVfile) as a:
first_line = a.readline()
with open(TEXTfile, 'w') as result:
result.write(first_line)
for line in a:
if line not in accessfile:
print line
但是,在文本文件,
中使用EMP_UPDATES.txt
分隔所有5条记录。
答案 0 :(得分:0)
使用DictReader阅读CSV
:
import csv
with open(CSVfile) as csvfile:
reader = csv.DictReader(csvfile)
csv_data = {(row['ID'], row['Name']) for row in reader} # generates set
如果预计只有CSV
包含缺失的数据:
diff = sorted(csv_data.difference(accessfile)) # Generated Sorted list with values missing
# in accessfile
要写入新文件:
with open(TEXTfile, 'w') as result:
for missing in diff:
result.write(";".join(missing))