Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python错误地写入csv文件_Python_Csv - Fatal编程技术网

Python错误地写入csv文件

Python错误地写入csv文件,python,csv,Python,Csv,我在Mac中尝试了我的代码,这是可以的,但在windows中,python没有将我的列表正确地写入CSV文件,一些数据被写入不同的行,而应该写入同一行 personel = [firstname, lastname, detail] with open("hr.csv", "a", newline='', encoding="utf-8") as csvfile: writer = csv.writer(csvfile) writer.writerow(personel) 我用

我在Mac中尝试了我的代码,这是可以的,但在windows中,python没有将我的列表正确地写入CSV文件,一些数据被写入不同的行,而应该写入同一行

personel = [firstname, lastname, detail]
with open("hr.csv", "a", newline='', encoding="utf-8") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(personel)

我用你的代码(在Windows 10上)编写了一个测试程序,但没有发现CSV输出有问题。在你的机器上运行这个,看看会发生什么

这是测试输出(由随机字母组成):

这是测试代码:

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
导入csv
随机输入
导入字符串
def随机字符串(stringLength=10):
字母=字符串。ascii_小写
返回“”。在范围(stringLength)内为i连接(随机选择(字母))
x=0
而(x<5):
personel=[randomString(),randomString(),randomString()]
将open(“hr.csv”,“a”,换行符=“”,encoding=“utf-8”)作为csvfile:
writer=csv.writer(csvfile)
编剧(个人)
x=x+1

我怀疑你的输入包含换行符。检查
firstname
lastname
详细信息中是否有多余的换行符。

我用您的代码编写了一个测试程序(在Windows 10上),但没有发现CSV输出有问题。在你的机器上运行这个,看看会发生什么

这是测试输出(由随机字母组成):

这是测试代码:

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
导入csv
随机输入
导入字符串
def随机字符串(stringLength=10):
字母=字符串。ascii_小写
返回“”。在范围(stringLength)内为i连接(随机选择(字母))
x=0
而(x<5):
personel=[randomString(),randomString(),randomString()]
将open(“hr.csv”,“a”,换行符=“”,encoding=“utf-8”)作为csvfile:
writer=csv.writer(csvfile)
编剧(个人)
x=x+1

我怀疑你的输入包含换行符。检查
firstname
lastname
详细信息中是否有多余的换行符。

尝试不将换行符指定为“”。@entresb你是什么意思?你能发布你的CSV得到了什么吗?我在测试代码中未发现CSV有问题。请尝试将换行符指定为,例如
\r\n
,而不是将其保留为空。另外,您使用的是哪一版本的python?我们需要您的变量所拥有的数据样本,否则我们在这里对您不会很有用…请尝试不将换行符指定为“”@entresb,这是什么意思?您能发布您的CSV得到了什么吗?我在测试代码中未发现CSV有问题。请尝试将换行符指定为,例如
\r\n
,而不是将其保留为空。另外,您使用的是哪一版本的python?我们需要您的变量所拥有的数据样本,否则我们在这里对您不会很有用。。。