循环浏览两个csv文件并检查每一行



我有两个csv文件,我试图在其中一个文件上循环以获得该行的值,然后将该行与另一个文件匹配,并检查每一行。例如:

'design_products_view.csv'

"id","name","price","product","thumbnail","thumbnail_url","template","description","active_description","stages","variations","attributes","printings","order","active","author","created","updated","template_id"
15,"1.0' Circle",0,0,"thumbnails/lumise-media-01_round-curled-sticker-PacerPlaceholder-12.png","https://design.pacerlabels.com/data/thumbnails/eRsp0afJYlN2.png","","Available in White BOPP - Clear BOPP - Silver Metallic BOPP - Holographic Metallic BOPP and Kraft.",0,"JTdCJTIyVHlONUJiSHVRcSUyMiUzQSU3QiUyMmVkaXRfem9uZSUyMiUzQSU3QiUyMmhlaWdodCUyMiUzQTM3OSUyQyUyMndpZHRoJTIyJTNBMzc5JTJDJTIybGVmdCUyMiUzQTAuNSUyQyUyMnRvcCUyMiUzQTAuNSUyQyUyMnJhZGl1cyUyMiUzQSUyMjEwMCUyMiU3RCUyQyUyMnVybCUyMiUzQSUyMnByb2R1Y3RzJTJGbDBuMHA5Y3Muc3ZnJTIyJTJDJTIyc291cmNlJTIyJTNBJTIydXBsb2FkcyUyMiUyQyUyMm92ZXJsYXklMjIlM0FmYWxzZSUyQyUyMnByb2R1Y3Rfd2lkdGglMjIlM0E0MDAlMkMlMjJwcm9kdWN0X2hlaWdodCUyMiUzQTQwMCUyQyUyMnRlbXBsYXRlJTIyJTNBJTdCJTdEJTJDJTIyc2l6ZSUyMiUzQSU3QiUyMndpZHRoJTIyJTNBJTIyMS4xMjUlMjIlMkMlMjJoZWlnaHQlMjIlM0ElMjIxLjEyNSUyMiUyQyUyMmNvbnN0cmFpbiUyMiUzQXRydWUlMkMlMjJ1bml0JTIyJTNBJTIyaW5jaCUyMiU3RCUyQyUyMmluY2x1ZGVfYmFzZSUyMiUzQWZhbHNlJTJDJTIyY3JvcF9tYXJrc19ibGVlZCUyMiUzQWZhbHNlJTJDJTIyb3JpZW50YXRpb24lMjIlM0ElMjJwb3J0cmFpdCUyMiUyQyUyMmxhYmVsJTIyJTNBJTIyU3RhcnQlMjBzdGFnZSUyMiU3RCU3RA==","JTdCJTIyZGVmYXVsdCUyMiUzQSUyMiUyMiUyQyUyMmF0dHJzJTIyJTNBJTVCJTVEJTJDJTIydmFyaWF0aW9ucyUyMiUzQSU3QiU3RCU3RA==","JTdCJTdE","%7B%7D",0,1,"","2022-02-11 22:38:44","2022-03-17 23:26:52",NULL
16,"1.063' Circle",0,0,"thumbnails/lumise-media-01_round-curled-sticker-PacerPlaceholder-13.png","https://design.pacerlabels.com/data/thumbnails/YSAsea9cRpdz.png","","Available in White BOPP - Clear BOPP - Silver Metallic BOPP - Holographic Metallic BOPP and Kraft.",0,"JTdCJTIyVHlONUJiSHVRcSUyMiUzQSU3QiUyMmVkaXRfem9uZSUyMiUzQSU3QiUyMmhlaWdodCUyMiUzQTM4MSUyQyUyMndpZHRoJTIyJTNBMzgxJTJDJTIybGVmdCUyMiUzQTAuNSUyQyUyMnRvcCUyMiUzQTAuNSUyQyUyMnJhZGl1cyUyMiUzQSUyMjEwMCUyMiU3RCUyQyUyMnVybCUyMiUzQSUyMnByb2R1Y3RzJTJGbDBuMHA5Y3Muc3ZnJTIyJTJDJTIyc291cmNlJTIyJTNBJTIydXBsb2FkcyUyMiUyQyUyMm92ZXJsYXklMjIlM0FmYWxzZSUyQyUyMnByb2R1Y3Rfd2lkdGglMjIlM0E0MDAlMkMlMjJwcm9kdWN0X2hlaWdodCUyMiUzQTQwMCUyQyUyMnRlbXBsYXRlJTIyJTNBJTdCJTdEJTJDJTIyc2l6ZSUyMiUzQSU3QiUyMndpZHRoJTIyJTNBJTIyMS4xODc1JTIyJTJDJTIyaGVpZ2h0JTIyJTNBJTIyMS4xODc1JTIyJTJDJTIyY29uc3RyYWluJTIyJTNBdHJ1ZSUyQyUyMnVuaXQlMjIlM0ElMjJpbmNoJTIyJTdEJTJDJTIyaW5jbHVkZV9iYXNlJTIyJTNBZmFsc2UlMkMlMjJjcm9wX21hcmtzX2JsZWVkJTIyJTNBZmFsc2UlMkMlMjJvcmllbnRhdGlvbiUyMiUzQSUyMnBvcnRyYWl0JTIyJTJDJTIybGFiZWwlMjIlM0ElMjJTdGFydCUyMHN0YWdlJTIyJTdEJTdE","JTdCJTIyZGVmYXVsdCUyMiUzQSUyMiUyMiUyQyUyMmF0dHJzJTIyJTNBJTVCJTVEJTJDJTIydmFyaWF0aW9ucyUyMiUzQSU3QiU3RCU3RA==","JTdCJTdE","%7B%7D",0,1,"","2022-02-11 23:09:44","2022-03-17 23:27:00",NULL

这里,第一个项目的行(不包括标题行(将是1.0,然后第二个项目将是1.063(还有很多(。

在第二个文件(templates_view.csv(中,我有以下内容:

"id","width","height","attribute_item_id","created_at","updated_at","design_id"
2584,1.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2585,1.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2586,2.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2587,2.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2588,2.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2589,2.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2590,3.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2591,3.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL 

我需要检查1.0是否在整个文件(templates_view.csv(的任何行中。我的代码是:导入csv

with open('design_products_view.csv') as design_file, open('templates_view.csv') as templates:
designs = csv.reader(design_file, delimiter=',')
template_files = csv.reader(templates, delimiter=',')
containsProduct = "Circle"
for row in designs:
if containsProduct in row[1]:
rowValue = row[1].split("'")[0]
for row in template_files:
if row[1] != "width":
print(rowValue)
if rowValue in row[1] and row[3] == "8":
print(row[1])

代码执行了,但我只通过了"design_products_view.csv"的第一行。print(rowValue(始终为1.0。我希望对于行中设计:将执行到完成,但事实并非如此。在初始项目之后,它停止。知道我做错了什么吗?

在内部循环一次后,文件迭代器将耗尽,因为您读取了整个文件。你需要选择

  1. 每次在执行内部循环之前都要重新打开内部循环的文件:
with open('design_products_view.csv') as design_file:
designs = csv.reader(design_file, delimiter=',')
containsProduct = "Circle"
for row in designs:
if containsProduct in row[1]:
rowValue = row[1].split("'")[0]
with open('templates_view.csv') as templates: # <---- this line!
template_files = csv.reader(templates, delimiter=',')
for row in template_files:
# do whatever
  1. 在启动内部循环之前先查找文件的开头,以便文件迭代器可以重新读取文件:
with open('design_products_view.csv') as design_file, open('templates_view.csv') as templates:
designs = csv.reader(design_file, delimiter=',')
template_files = csv.reader(templates, delimiter=',')
containsProduct = "Circle"
for row in designs:
if containsProduct in row[1]:
rowValue = row[1].split("'")[0]
templates.seek(0)                # <---- this line!
for row in template_files:
# do whatever
  1. 将csv文件读取到列表中并使用该列表:
with open('design_products_view.csv') as design_file, open('templates_view.csv') as templates:
designs = list(csv.reader(design_file, delimiter=','))        # <---- this line!
template_files = list(csv.reader(templates, delimiter=','))   # <---- this line!
containsProduct = "Circle"
for row in designs:
if containsProduct in row[1]:
rowValue = row[1].split("'")[0]
for row in template_files:
# do whatever

如果你有足够的内存,我建议你选择第三个选项,因为这会减少文件IO的速度。

相关内容

  • 没有找到相关文章

最新更新