我正在尝试匹配两个文件中的字符串对。但是,如果我测试循环,它将显示意外的(不完整的)输出。
我使用下面的代码。它只运行一次嵌套循环,而我希望它可以在每个普通循环中运行。
def csv_remove_header(input_file):
return next(input_file)
def csv_clean_up(line):
# remove line breaks
no_line_breaks = line.rstrip('\n')
# split columns by ';'
split_element = no_line_breaks.split(';')
return(split_element)
with open ('file1.csv', 'r') as input_1:
csv_remove_header(input_1)
with open ('file2.csv', 'r') as input_2:
csv_remove_header(input_2)
for i in input_1:
clean_line_i = csv_clean_up(i)
key_i = clean_line_i[0]
print(key_i)
for j in input_2:
clean_line_j = csv_clean_up(j)
key_j = clean_line_j[0]
print(key_j)
这将输出预期结果:
a = [1, 3, 6, 7, 6, 7]
b = [12, 1, 123, 6, 565, 5, 6598, 78987, 4, 654, 9, 7, 8, 6, 12, 3, 56]
for i in a:
for j in b:
if i == j:
print(i, j, True)
else:
print(i, j, False)