【发布时间】:2017-11-15 15:31:09
【问题描述】:
我编写了一个 python 脚本来验证报告模板。有一个主模板,其中包含所有可能的报告及其所有可能的字段。然后是一个包含其中一些报告的模板 csv。为简单起见,让我们说看起来像这样;
模板 csv:
OutputName, col1, col2, col3, col4, col5
PersonReport, name, surname, age, dob, id
AccountReport, f1, f2, f3, f4, f5
TransactionReport, f1, f2, f3, f4, f5
主 csv:
OutputName, col1, col2, col3, col4, col5
PersonReport, name, surname, street, age, id
TransactionReport, f1, f2, f3, f4, f5
所以在这个例子中,AccountReport 甚至不存在于 Master 中,PersonReport 包含一个无效的字段 dob,因为它不在 Master 中。唯一有效的报告是TransactionReport。
所以我的想法是将这些 csvs 作为字典读取,OutputName 作为键,字段名称作为值。
import pandas as pd
masterDf = pd.read_csv('master.csv')
master = masterDf.set_index('OutputName').T.to_dict('list')
templateDf = pd.read_csv('template.csv')
template = templateDf.set_index('OutputName').T.to_dict('list')
字典看起来像;
template = {' PersonReport': [' name', ' surname', ' age', ' dob', ' id'], ' AccountReport': [' f1', ' f2', ' f3', ' f4', ' f5 '], ' TransactionReport': [' f1', ' f2', ' f3', ' f4', ' f5']}
master = {' PersonReport': [' name', ' surname', ' street', ' age', ' id'], ' TransactionReport': [' f1', ' f2', ' f3', ' f4', ' f5']}
现在我想首先匹配键,找出主字典中不存在哪些键。之后,当找到匹配的键时,我希望检查字典中的值是否有效,方法是检查它们是否存在于主字典的值中。
所以我试试:
errorCount = 0
for key, value in template.items():
if key not in master:
print("{} is an invalid report".format(key))
errorCount += 1
if key in master:
for fields in template.values():
for field in fields:
for cols in master.values():
if field not in cols:
print("{} is an invalid field in {} report".format(field, key))
errorCount += 1
但是我得到的输出是错误的。我明白了:
name is an invalid field in PersonReport report
surname is an invalid field in PersonReport report
age is an invalid field in PersonReport report
dob is an invalid field in PersonReport report
dob is an invalid field in PersonReport report
id is an invalid field in PersonReport report
f1 is an invalid field in PersonReport report
f2 is an invalid field in PersonReport report
f3 is an invalid field in PersonReport report
f4 is an invalid field in PersonReport report
f5 is an invalid field in PersonReport report
f5 is an invalid field in PersonReport report
f1 is an invalid field in PersonReport report
f2 is an invalid field in PersonReport report
f3 is an invalid field in PersonReport report
f4 is an invalid field in PersonReport report
f5 is an invalid field in PersonReport report
AccountReport is an invalid report
name is an invalid field in TransactionReport report
surname is an invalid field in TransactionReport report
age is an invalid field in TransactionReport report
dob is an invalid field in TransactionReport report
dob is an invalid field in TransactionReport report
id is an invalid field in TransactionReport report
f1 is an invalid field in TransactionReport report
f2 is an invalid field in TransactionReport report
f3 is an invalid field in TransactionReport report
f4 is an invalid field in TransactionReport report
f5 is an invalid field in TransactionReport report
f5 is an invalid field in TransactionReport report
f1 is an invalid field in TransactionReport report
f2 is an invalid field in TransactionReport report
f3 is an invalid field in TransactionReport report
f4 is an invalid field in TransactionReport report
f5 is an invalid field in TransactionReport report
我的预期输出是:
AccountReport is an invalid report
dob is an invalid field in PersonReport report
任何帮助表示赞赏 谢谢 ps我使用的是python 3.6
【问题讨论】:
-
发布预期输出
-
已经完成,谢谢@RomanPerekhrest
标签: python python-3.x csv dictionary