python语言csv文件读写

读取csv文件数据

import csv

csv_reader = csv.reader(open('factor/daily_quote.csv'))

文本文件的方式读取csv文件

import csv
import io
#文件格式是utf-8,分隔符是`$`
fileName = '/work/tianchi/美年健康AI大赛—双高疾病风险预测/数据/[new] meinian_round1_test_a_20180409.csv'
with io.open(fileName, "r", encoding='utf-8') as csv_file:
    reader = csv.reader(csv_file, delimiter='$')
    header = next(reader)
    print('=========================')  # 分隔行
    print(header)
    data = [row[1] for row in reader]

    #关闭io连接
    csv_file.close()

#csv文件的行数
print(len(data))

      

写数据到csv文件

#newline = '' 防止出现换行
out = open(outfile, 'w', newline='')
csv_writer = csv.writer(out)
csv_writer.writerow(list)

#关闭文件连接
out.close()

email:[email protected]

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