我被一个小问题困在这里。我的电脑上运行着一个 python tcp 服务器,从客户端接收数据。我需要自动写入 csv 文件。现在唯一的问题是第一行中的数据被覆盖,有没有办法为我正在接收的数据提供索引?帮助会很大。谢谢!
#while connected
while 1:
data = con.recv(Buffer_size)
writer = csv.writer(open(filename+".csv", 'w')
writer.writerow('\n'data)
最佳答案
您将参数'w'
发送到open()
。 'w'
表示
w: open for writing, truncating the file first
( https://docs.python.org/3/library/functions.html#open )
将其更改为'a'
a: open for writing, appending to the end of the file if it exists
您将附加到文件末尾。
即
#while connected
while 1:
data = con.recv(Buffer_size)
writer = csv.writer(open(filename+".csv", 'a')
writer.writerow('\n'data)
关于python - 在 python 中写入 csv 文件时有没有办法使用索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810780/