我编写了一个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
甚至不存在于主节点中,PersonReport
包含一个字段{TransactionReport
。在
所以我的想法是将这些csv作为字典读入,其中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']}
现在,我想首先匹配这些键,找出哪些键不存在于主dict中。之后,当查找匹配的键时,我希望通过检查主dict的值中是否存在来检查字典中的值是否有效。在
所以我试着:
^{pr2}$但是输出是错误的。我得到:
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
感谢任何帮助 谢谢 p、 使用python I的6.6 m
你可以试试这个:
输出:
^{pr2}$我保持简单,遵循你们的惯例:
从控制台:
^{pr2}$你的代码有几个错误导致你检查每个字段。使用
continue
并只迭代正确的键就解决了这个问题。 下面是一个固定的代码片段:该输出:
^{pr2}$相关问题 更多 >
编程相关推荐