[英]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.