【发布时间】:2016-02-11 07:57:03
【问题描述】:
list1 = Csvfile1._getRow(' field1')
list2 = Csvfile2._getRow(' field1')
_list1 = Csvfile1._getRow(' field2')
_list2 = Csvfile2._getRow(' field2')
for i,(a,b) in enumerate(zip(list2, list1)):
value = False
if field == ' field1':
for j,(c,d) in enumerate(zip(_list2, _list1)):
if i == j:
if a != b and c != d:
value = True
else:
value = False
break
if value == True:
continue
if a != b
# do something
以下是示例: 比较两个 csv 文件中的值。当 field1 的值 在两个 csv 文件中不相等,条件 if a != b: 应该被执行。
当两个 csv 文件中 field1 的值不相等时,同时如果 field2 的值也不相等 -> 则不应执行条件 if a != b:。
对于庞大的数据,这似乎行不通。还是有更好的方法来实现这一点?
Csvfile1
字段1 |字段2
222 | 4 -> 输入 if a != b: 条件循环
435 | 5 -> 如果 a != b: 条件循环则不要输入
Csvfile2
字段1 |字段2
223 | 4
436 | 6
【问题讨论】:
-
field设置在哪里? -
让我们假设代码是:list1 = Csvfile1._getRow(field) list2 = Csvfile2._getRow(field1)。字段作为参数传递给此函数。
-
仅在比较该特定字段的值时 - 需要此“额外检查”。对于其他字段,直接比较,不相等则执行a!=b条件
标签: python csv for-loop enumerate