csv

2020-12-04  本文已影响0人  xncode

csv comma separated values
尽管分界符和转义符可能不一样,但因为这种格式还是足够简单,所以能提供一个统一的模块

csv.reader(csvfile, dialect='excel', **fmtparams)

返回的reader可被迭代

如果传入的csvfile是文件对象,则需要其在打开时指定newline=""

每行被读入的都视为字符串列表,但如果传入了QUOTE_NONNUMERIC则会自动转换

import csv
with open('eggs.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print(', '.join(row))

next

dialect

line_num

fieldnames

csv.writer(csvfile, dialect='excel', **fmtparams)

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

writerow

writerows

dialect

writeheader

register_dialect

unregister_dialect

get_dialect list_dialects

field_size_limit

DictReader DictWriter

Dialect

上一篇下一篇

猜你喜欢

热点阅读