我试图借助python将数据保存到csv文件中。尽管数据正在保存,但是当我在终端中运行代码时却显示出一些奇怪的模式。 这是我的python代码
import MySQLdb
import csv
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="", # your password
db="getinvolved") # name of the data base
# you must create a Cursor object. It will let
# you execute all the queries you need
cur = db.cursor()
# Use all the SQL you like
cur.execute("SELECT name,phoneNo,email FROM getinvolved")
# print all the first cell of all the rows
dblist=cur.fetchall()
length=len(dblist)-1
lastRow=dblist[length]
print(lastRow[0],lastRow[1],lastRow[2])
fieldname={'name','phoneNo','email'}
with open("emailsend.csv","a") as csvfile:
writer=csv.DictWriter(csvfile,fieldnames=fieldname)
writer.writeheader()
writer.writerow({
"name":lastRow[0],
"phoneNo":lastRow[1],
"email":lastRow[2]
})
**这是我的csv文件,显示随机模式。每次将值存储在其中时,我也会得到一个空行。标头名称本身就是在玩杂耍**
name,phoneNo,email
asdsfd,9876543,abc@hotmail.com
phoneNo,email,name
9876543,abc@hotmail.com,asdsfd
email,phoneNo,name
abc@hotmail.com,9876543,asdsfd
email,phoneNo,name
abc@hotmail.com,9876543,asdsfd
name,phoneNo,email
asdsfd,9876543,abc@hotmail.com
name,email,phoneNo
asdsfd,abc@hotmail.com,9876543
我希望它像下面那样保存我的数据,每次添加新值时都没有空白行
name,email,phoneNo
asdsfd,abc@hotmail.com,9876543
name,email,phoneNo
asdsfd,abc@hotmail.com,9876543
name,email,phoneNo
asdsfd,abc@hotmail.com,9876543