python - 在 python 中写入 csv 文件时有没有办法使用索引

标签 python python-3.x python-2.7

我被一个小问题困在这里。我的电脑上运行着一个 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/

相关文章:

python - C 扩展模块中的 printf 比 python 中的 writelines 更快吗?

python - web2py 中的复合键

python - 为什么是一个包罗万象的尝试 :/except: not enough to catch an exception?

python - 如何从 Django 阻止列表中删除用户(用户被 Django throttle 阻止)?

python - __init__() 的类型错误

javascript - 将字符串从 Python 传递到 Javascript

python:模块没有属性机械化

python - mongoengine中的EmbeddedDocumentField和ReferenceField

python - 将文档逐行追加到列表中

python - 如何在 lambda 函数中执行赋值