熊猫引用参数

时间:2018-12-19 21:08:34

标签: python pandas

我试图确切地了解pandas.read_csv模块中quotingdoublequote的含义。假设我有以下数据:

['name' ,'age' ,'position']
['tom', 14, 'vp']
['jared', 100, 'head, sales']

熊猫有三个quoting选项:

  1. QUOTE_MINIMAL(0)[默认]

  2. QUOTE_ALL(1)

  3. QUOTE_NONNUMERIC(2)

  4. QUOTE_NONE(3)

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

如果quotechar",以上四个参数将如何解释以上数据?

1 个答案:

答案 0 :(得分:3)

您可以尝试执行以下操作,以与csv作家进行测试:

import csv
DATA = [
    ['name' ,'age' ,'position'],
    ['tom', 14, 'vp'],
    ['jared', 100, 'head, sales'],
]

with open('test_min.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for row in DATA:
        writer.writerow(row)

with open('test_all.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_ALL)
    for row in DATA:
        writer.writerow(row)

with open('test_nonnumeric.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
    for row in DATA:
        writer.writerow(row)

with open('test_quotenone.csv', 'w') as csvfile:
    writer = csv.writer(csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_NONE)
    for row in DATA:
        writer.writerow(row)

这是您将看到的:

  1. QUOTE_NONE

    name,age,position tom,14,vp

    注释:无效的输出,将需要一个转义字符集。

  2. QUOTE_NONNUMERIC

    "name","age","position" "tom",14,"vp" "jared",100,"head, sales"

    注意事项:请注意,14100并未转义。

  3. QUOTE_MINIMUM

    name,age,position tom,14,vp jared,100,"head, sales"

    注释:仅引用head, sales,因为它是有问题的字段。

  4. QUOTE_ALL

    "name","age","position" "tom","14","vp" "jared","100","head, sales"

    注释:无论类型如何,所有内容都被引用。