> lastname,firstname,org,title,phone,email,website,street,city,p_code,country
> Doe,John,John Doe plc,Web Developer,143893456,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4aeabacaaeaa0aba184acaea0eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>,https://johndoe.com, 203 East 50th Steet,New York,10022,USA
> Morgan,Peter,Pythonfactory Inc.,Backend Developer,141996746,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b4a1b0a1b6eaa9abb6a3a5aa84acb4bdb0acabaaa2a5a7b0abb6bdeaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>,https://pythonfactory.com,203 Weststeet,New York,10022,USA
import pyqrcode
import pandas as pd
def createQRCode():
df = pd.read_csv("havas.csv")
for index, values in df.iterrows():
lastname = values["lastname"]
firstname = values["firstname"]
title = values["title"]
phone = values["phone"]
email = values["email"]
website = values["website"]
org = values["org"]
street = values["street"]
city = values["city"]
p_code = values["p_code"]
country = values["country"]
data = f'''
"BEGIN:VCARD\n"
"N:{lastname};{firstname};\n"
"FN:{lastname}+{firstname}\n"
"TITLE:{title}\n"
"TEL;TYPE=work,VOICE:{phone}\n"
"EMAIL;WORK;INTERNET:{email}\n"
"URL:{website}\n"
"ORG:{org}\n"
"ADR;TYPE=work,PREF;;;{street};{city};{p_code};{country}\n"
"VERSION:3.0\n"
"END:VCARD\n"
'''
image = pyqrcode.create(data)
image.svg(f"{lastname}_{firstname}.svg", scale="5")
createQRCode()
我有一个 CSV 文件,其中包含多名员工。我想从这个文件中为每个员工生成一个 Vcard QR 码。不幸的是,扫描 QRCode 时仅检索到 URL。
不幸的是,我没有意识到这个错误,因为我对 Python 缺乏经验。我将非常感谢您的帮助!
最佳答案
由于您使用的是 Python 多行字符串,因此不需要包含换行符、额外的引号和缩进。请尝试以下操作:
import pyqrcode
import pandas as pd
def createQRCode():
df = pd.read_csv("havas.csv")
for index, values in df.iterrows():
lastname = values["lastname"]
firstname = values["firstname"]
title = values["title"]
phone = values["phone"]
email = values["email"]
website = values["website"]
org = values["org"]
street = values["street"]
city = values["city"]
p_code = values["p_code"]
country = values["country"]
data = f'''BEGIN:VCARD
N:{lastname};{firstname};
FN:{lastname}+{firstname}
TITLE:{title}
TEL;TYPE=work,VOICE:{phone}
EMAIL;WORK;INTERNET:{email}
URL:{website}
ORG:{org}
ADR;TYPE=work,PREF;;;{street};{city};{p_code};{country}
VERSION:3.0
END:VCARD'''
image = pyqrcode.create(data)
image.svg(f"{lastname}_{firstname}.svg", scale="5")
createQRCode()
这将创建以下二维码:
您可以执行以下操作以避免需要额外的变量:
import pyqrcode
import pandas as pd
def createQRCode():
df = pd.read_csv("havas.csv")
for index, v in df.iterrows():
data = f'''BEGIN:VCARD
N:{v['lastname']};{v['firstname']};
FN:{v['lastname']}+{v['firstname']}
TITLE:{v['title']}
TEL;TYPE=work,VOICE:{v['phone']}
EMAIL;WORK;INTERNET:{v['email']}
URL:{v['website']}
ORG:{v['org']}
ADR;TYPE=work,PREF;;;{v['street']};{v['city']};{v['p_code']};{v['country']}
VERSION:3.0
END:VCARD'''
image = pyqrcode.create(data)
image.svg(f"{v['lastname']}_{v['firstname']}.svg", scale="5")
createQRCode()
关于python - 如何在 Python 中从 CSV 文件生成多个 Vcard QRCode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69853641/