列表中的字典数据写入到csv表

def writeData():
	rows = self.info_list()	# 格式为 [{}]
	if not os.path.exists('userinfo.csv'):
	     with open('userinfo.csv','a', newline='', encoding='utf-8') as f:
	         headers = 'surname,firstName,contactPhoneNo,emailAddress,loginId,password'
	         f.writelines(headers)
	         f.writelines('\n')
	         f.writelines("%s,%s,%s,%s,%s,%s" % (rows[0]["surname"],rows[0]["firstName"],rows[0]["contactPhoneNo"],rows[0]["emailAddress"],rows[0]["loginId"],rows[0]["password"],))
	         f.writelines('\n')	         
	else:
	    with open('userinfo.csv', 'a', newline='', encoding='utf-8') as f:
	        f.writelines("%s,%s,%s,%s,%s,%s" % (
	        rows[0]["surname"], rows[0]["firstName"], rows[0]["contactPhoneNo"], rows[0]["emailAddress"],
	        rows[0]["loginId"], rows[0]["password"],))
	        f.writelines('\n')


if __name__ == "__main__":
	writeData()

你可能感兴趣的:(python基础)