简体   繁体   English

如何遍历CSV并使用每个行字符串创建QR码-PYTHON

[英]How to Loop through a CSV and use each Row String to Create a QR Code - PYTHON

I am trying to write a code that will loop through a CSV file I have, combine the two pieces of data ( In this case "Rep" and "Entry") and then create a QR Code for each returned value... I have figured out how to make the QR code and how to Combine the data, but I cannot figure out the loop and how to put all of this together. 我正在尝试编写一个代码,该代码将遍历我拥有的CSV文件,合并两段数据(在这种情况下为“ Rep”和“ Entry”),然后为每个返回的值创建一个QR代码...想出了如何制作QR码以及如何合并数据,但是我无法弄清楚循环以及如何将所有这些放在一起。 Thank you for any help! 感谢您的任何帮助!

import csv
import qrcode

with open('SLS_labels.csv') as csvfile:
    fieldnames= ["Rep", "Entry"]
    reader= csv.reader(csvfile)

    for row in reader:
        labeldata = row[0] + row[1]
        print labeldata


    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=1,
        border=4,
    )

    qr.add_data(labeldata)
    qr.make(fit=True)

    img = qr.make_image()
    img.save("test.jpg")

You are going to want to create you label within the for loop: 您将要在for循环中创建标签:

for row in reader:
    labeldata = row[0] + row[1]

    qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=1,
    border=4)

    qr.add_data(labeldata)
    qr.make(fit=True)

    img = qr.make_image()
    img.save(labeldata+".jpg") #this assumes your label data would make a good file name

Alternatively you could add all of your labeldata to a list and iterate over that... 或者,您可以将所有labeldata添加到列表中并对其进行迭代...

labeldata = []
for row in reader:
    labeldata += [row[0] + row[1]]
...
for label in labeldata:
    #make labels
import csv
import qrcode

with open('SLS_labels.csv') as csvfile:
    fieldnames= ["Rep", "Entry"]
    reader= csv.reader(csvfile)

    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=1,
        border=4,
    )

    for i, row in enumerate(reader):
        labeldata = row[0] + row[1]
        print labeldata

        qr.add_data(labeldata)
        qr.make(fit=True)

        img = qr.make_image()
        img.save("test{}.jpg".format(i))

I added enumerate so you'd also get an index number for your file names instead of having multiple test.jpg . 我添加了枚举,因此您也将获得文件名的索引号,而不是拥有多个test.jpg

If you want to clear the data added you can call qr.clear() after img.save() 如果要清除添加的数据,可以在qr.clear()之后调用img.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历列表并将列表的每个字符串与 csv 文件中的一行进行比较 [Python] - Loop through a list and compare each string of a list to a row in a csv file [Python] Python - 如何遍历列中的唯一值,创建数据框并为每个值输出到 csv - Python - how to loop through unique values in column, create dataframe and output to csv for each one 为什么我的 Python CSV 代码在每一行中都以字符串形式读取? - Why is my Python CSV code reading in each row as a string? 如何循环遍历 CSV 文件中的一行 Python? - How do I loop through one row of a CSV file in Python? 使用python,如何在csv文件的每一行中拆分字符串 - With python, how to split string in each row of a csv file Python - 循环遍历csv文件的行值 - Python - loop through a csv file row values Django / Python:每次通过CSV for-in循环覆盖第一行 - Django/Python: CSV for-in loop overriding first row each time through 当for循环将function应用于每一行时,如何在python中使用多处理 - how to use multiprocessing in python when a for loop applies function to each row 如何在张量流中遍历张量的每一行 - how to loop through each row in a tensor in tensorflow 在 Python 中,通过测试运行 csv 中的每一行,并在 output 中运行一个新的 Z628CB5675FF524F3EZE719 - In Python, run each row in a csv through tests and output a new csv showing which test each row failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM