python中操作csv文件 读取csv improt csv f = csv.reader(open("文件路径","r")) for i in f: print(i) 结果会以列表形式输出 写入csv文件 import csv data = [ ('xxx','xxxx','xxxx'), ('xxx','xxxx','xxxx'), ('xxx','xxxx','xxxx') ] f = open('文件路径', 'w') # 创建文件操作对象 writer
一.前言 一般来说,为了方便,使用python的时候都会使用csv模块去写数据到csv文件,但是写入中文的时候,经常会报错: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128) 我试过直接用utf8编码打开文件,然后直接将要写入的字段拼接为逗号分隔的字符串,虽然能解决中文写入的问题,但是操作很麻烦.而且直接使用excel打开的时候,还是会显示乱码.
1.什么是csv文件 The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. CSV format was used for many years prior to attempts to describe the format in a standardized way in RFC 4180. 2.