csv

作者: xncode | 来源:发表于2020-12-04 16:13 被阅读0次

    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

    相关文章

      网友评论

          本文标题:csv

          本文链接:https://www.haomeiwen.com/subject/mbaxextx.html