python3 csv 中newline的问题
先附上代码import csv
import random
import time
t1=time.time()
for i in range(10000):
t1 = random.randint(1, 999)
t2 = random.randrange(999, 1999)
# print(t1, t2)
with open("删除.csv", "a", encoding="utf-8") as f:
file = csv.writer(f, delimiter="|",newline=" ")
file.writerows()
t2=time.time()
print(t2-t1)
如上,加上newline=" "就报错
D:\Python38\python.exe E:/Python_files/-打发无聊时光-/csv练习.py
Traceback (most recent call last):
File "E:/Python_files/-打发无聊时光-/csv练习.py", line 11, in <module>
file = csv.writer(f, delimiter="|",newline=" ")
TypeError: 'newline' is an invalid keyword argument for this function
如果不加,可以正常运行,但是每个一个数据就有一个空行,很麻烦。且不美观。
请问我的问题出在哪里 python3.8
谢谢 with open("删除.csv", "a", encoding="utf-8",newline='') as f:
file = csv.writer(f, delimiter="|")
file.writerows()
newline是open()函数的参数 import csv
import random
import time
t1=time.time()
for i in range(10000):
t1 = random.randint(1, 999)
t2 = random.randrange(999, 1999)
# print(t1, t2)
with open("删除.csv", "a", encoding="utf-8",newline='') as f:
file = csv.writer(f, delimiter="|")
file.writerows()
t2=time.time()
print(t2-t1)
百度newline要放到open里面,经测试能解决 读import csv
filename = '名单.csv'
with open(filename) as f:
reader = csv.reader(f)
print(list(reader))
写
import csv
filename = '名单.csv'
csvFile = open(filename, "w", encoding='utf-8', newline='')
writer = csv.writer(csvFile)
fileHeader = ("姓名", "年龄")
writer.writerow(fileHeader)
data = ("张三",17)
writer.writerow(data)
csvFile.close() 感谢各位。一句话点醒梦中人。
页:
[1]