在python中解析两种不同类型的csv格式

2024-09-29 02:24:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个csv文件,每个文件都有不同的格式。这里是两个不同的csv文件的示例。请看格式而不是数值。你知道吗

 csv_2   "xxxx-0147-xxxx-194443,""Jan 1, 2017"",7:43:43 AM PST,,Google fee,,Smart Plan (Calling & Texting),com.yuilop,1,unlimited_usca_tariff_and,mimir,US,TX,76501,USD,-3.00,0.950210,EUR,-2.85"
 csv_2  "1305-xxxx-0118-54476..1,""Jan 1, 2017"",7:17:31 AM PST,,Google fee,,Smart Plan (Calling & Texting),com.yuilop,1,unlimited_usca_tariff_and,htc_a13wlpp,US,TX,79079,USD,-3.00,0.950210,EUR,-2.85"
 csv_1 GPA.xxxx-2612-xxxx-44448..0,2017-02-01,1485950845,Charged,m1,Freedom Plan (alling & Texting),com.yuilop,subscription,basic_usca_tariff_and,USD,2.99,0.00,2.99,,,07605,US
 csv:1 GPA.xxxx-6099-9725-56125,2017-02-01,1485952917,Charged,athene_f,Buy 100 credits (Calling & Texting),com.yuilop,inapp,100_credits,INR,138.41,0.00,138.41,Kolkata,West Bengal,700007,IN

如你所见,csv琰u 2包含在“有时”,但是csv琰u 1是一种简单的格式。我得到所有的CSV的需求,他们是很多和巨大的。我试着用嗅探器来自动识别方言。但这是不够的,我没有得到一个有“”的合理的反应。有没有人能指导我如何解决这个问题?你知道吗

Python code 2.7

With open(file, 'rU') as csvfile:
     dialect = csv.Sniffer().sniff(csvfile.read(2024))
     csvfile.seek(0)
     reader = csv.reader(csvfile, dialect)
     for line in reader:
      print line
^{2}$
 dialect.escapechar     None
 dialect.quotechar      "
 dialect.quoting        0
 dialect.escapechar     None
 dialect.delimiter      ,
 dialect.doublequote    False

result

csv_1 ['GPA.13xx-xxxx-9725-5xxx', '2017-02-01', '1485952917', 'Charged', 'athene_f', 'Buy 100 credits (Calling & Texting)', 'com.yuilop', 'inapp', '100_credits', 'INR', '138.41', '0.00', '138.41', 'Kolkata', 'West Bengal', '700007', 'IN']
csv_2  ['1330-xxxx-5560-xxxx,"Jan 1', ' 2017""', '12:35:13 AM PST', '', 'Google fee', '', 'Smart Plan (Calling & Texting)', 'com.yuilop', '1', 'unlimited_usca_tariff_and', 'astar-y3', 'US', 'NC', '27288', 'USD', '-3.00', '0.950210', 'EUR', '-2.85"']

在csvè2中,你会看到一片混乱。日期由逗号分隔,特别是日期字段和所有被视为字符串的行。如何更改代码以获得与csv\u 1相同的结果?你知道吗


Tags: andcsvcsvfilecomususdcreditsplan
2条回答

为什么不先对csv文件进行预处理,将其清理并规范化,然后像其他csv文件一样加载数据呢?你知道吗

你离工作代码只有一步之遥。你所要做的就是首先replacecsvfile中的",然后你当前的方法就可以正常工作了。你知道吗

编辑:但是,如果您对合并在CSV文件中读取后分开的日期字符串感兴趣,那么您最好选择正则表达式匹配。我在原始答案中加入了一些代码。我已经从this older answer复制了大部分正则表达式代码(带有编辑)。你知道吗

import re
import csv

with open(file, 'rU') as csvfile:
    data = csvfile.read(2024)
    # Remove the pesky double-quotes
    no_quotes_data = data.replace('"', '')

    dialect = csv.Sniffer().sniff(no_quotes_data);

    csv_data = csv.reader(no_quotes_data.splitlines(), dialect)

    pattern = r'(?i)(%s) +(%s)'

    thirties = pattern % (
        "Sep|Apr|Jun|Nov",
        r'[1-9]|[12]\d|30')

    thirtyones = pattern % (
        "Jan|Mar|May|Jul|Aug|Oct|Dec",
        r'[1-9]|[12]\d|3[01]')

    feb = r'(Feb) +(?:%s)' % (
        r'(?:([1-9]|1\d|2[0-9]))') # 1-29 any year (including potential leap years)

    result = '|'.join('(?:%s)' % x for x in (thirties, thirtyones, feb))
    r = re.compile(result)

    for ind, phrase in enumerate(csv_data):
        if r.match(phrase):
            # If you've found a date string, a year string will follow
            new_data[ind] = ", ".join(csv_data[ind:ind+2])
            del csv_data[ind+1]

    for line in csv_data: print line

相关问题 更多 >