写入/读取csv文件(使用Python的csv包)

写入:

with open(qa_csv_path, "w") as csv_file:
# 设定写入模式
csv_write = csv.writer(csv_file, dialect='excel')
for l in write_list:
csv_write.writerow(l)

读取:
with open(data_dir, "r") as f:
csv_file = csv.reader(f)
for line in csv_file

    print(line)
 

pd.read_csv()方法中header参数,默认为0,标签为0(即第1行)的行为表头。若设置为-1,则无表头。示例如下:

(1)不设置header参数(默认)时:

df1 = pd.read_csv('target.csv',encoding='utf-8')
df1

(2)header=1时:

import pandas as pd
df2 = pd.read_csv('target.csv',encoding='utf-8',header=1)
df2
  

(3)header=-1时(可用于读取无表头CSV文件):

df3 = pd.read_csv('target.csv',encoding='utf-8',header=-1)
df3
 


 
原文地址:https://www.cnblogs.com/gagaein/p/14801750.html