I tried my code in Mac, and it is ok, but in windows, python does not write my list correctly into a CSV file, and some data is written in different rows while it should be written in the same row.
personel = [firstname, lastname, detail]
with open("hr.csv", "a", newline='', encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(personel)
I wrote a test program with your code (on Windows 10) and didn't find a problem with the CSV output. Run this on your machine and see what happens.
This is the test output (it consists of random letters):
fziziszaoq,gxlcgmncdn,bjehjbcjgq
lrzzbujdru,bxxqkavqro,mktmygqmrc
mauzgqkgqw,uijnzavbyi,jhjqxkbran
oplrispslo,irvkjgcimm,kjbfbpilmi
regaajxhdf,wwtyyspuoi,icmrcjwswx
This is the test code:
import csv
import random
import string
def randomString(stringLength=10):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))
x = 0
while(x < 5):
personel = [randomString(), randomString(), randomString()]
with open("hr.csv", "a", newline='', encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(personel)
x = x + 1
My suspicion is that your inputs contain newlines. Check that there are not stray newline characters in firstname
, lastname
, or detail.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.