Python的csv文件写入注意点writerow和writerows时--------------里面是用()还是[]

第一种写入,用的是()
注意这里面的writerows使用的()—开始我因为最后得到的行是tuple元组,最后经过实验发现,仍旧是list列表

with open(r'D:\lagua\study\coding\pythonPractice\test.csv','w',newline='',encoding = 'utf-8') as f:
    l1 = [0,1,2]
    l2 = [1,2,3]
    l3 = [2,3,4]
    writer = csv.writer(f)
    writer.writerows((l1,l2,l3))

第二种写入,用的是[]

with open(r'D:\lagua\study\coding\pythonPractice\test.csv','w',newline='',encoding = 'utf-8') as f:
    l1 = [0,1,2]
    l2 = [1,2,3]
    l3 = [2,3,4]
    writer = csv.writer(f)
    writer.writerows([l1,l2,l3])

经过实验,发现两者得到的均为list–指的是取数据时,第一行是list
测试代码如下

with open(r'D:\lagua\study\coding\pythonPractice\test.csv','r',newline='',encoding = 'utf-8') as f:
    reader = csv.reader(f)
    temp = []
    for line in reader:
        temp.append(line)
print(isinstance(temp[0],list))

结果为True

你可能感兴趣的:(python,文件读写,python,csv)