【问题标题】:How to parse out a dataframe from .csv file. which contains header detail rows using Python如何从 .csv 文件中解析出数据框。其中包含使用 Python 的标题详细信息行
【发布时间】:2020-08-07 10:50:43
【问题描述】:

我有一个文件,我试图从中提取值以创建数据框。我尝试了一种正则表达式方法从文件创建列表,但是当我将结果列表输入数据框时,如下所示的数据格式(标题/H 和详细信息/D)给了我不一致的行数。我认为问题在于某些记录有 1 个详细信息 (D) 行,而其他记录有超过 1 个 (D) 行。你能建议另一种方法吗?我正在考虑尝试创建一个字典对象,其中每个 H 行将是键,每个 D 行将是值,使用某种 for 循环。

文件格式如下:

H, INV34801, 20200201, 09:18:55, IN, 5 D, INV34801, 0053, 1.00, IN, 20200201, 09:18:55,
H, INV34802, 20200201, 10:12:35, IN, 5 D, INV34802, D22345433DU, -1.00, IN, 20200201, 10:12:35,
D, INV34802, , 1.00, IN, 20200201, 10:12:35,

这是我一直在尝试的代码:

    import pandas as pd
    import re
    import itertools
    #First I extract the date that each sale took place. 
    lst1= [line for line in re.findall(('[IN, ]\d\d\d\d\d\d\d\d'), contents)]
    #Now I remove every alternate date to remove the duplicate date I can confirm seeing that the                                 
    #date column has the same number of rows as the Invoice Number column
    lst1=lst1[1::2]
    #Now I extract the invoice number
    lst2= [line for line in re.findall("INV\w*",contents)]
    # Now I extract the product codes
    lst3=[line for line in         
    re.findall(('\s\s\s\s\s\w\w\w\w\w\w\w\w\w\w\w|\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s|\n
    \s\s\s\s\s\s\s\s\s\s\s\s\d\d\d\d|\s\s\s\s\s\s\s\s\s\s\d\d\d\d\d\d'),contents)]
    #Now I extract the Quantity Sold 
    lst4=[line for line in re.findall(('\s\s\s\s\s\s\d\.\d\d'),contents)]
    #then I create a column from the list of Invoice numbers
    df=pd.DataFrame([lst1,lst2,lst3,lst4])
    df =df.transpose()
    df.columns=['Date','Invoice_Number','Product_Code','Quantity']
    print(df)
    ''' 

我得到的输出结构是正确的,但数量和产品代码与正确的发票编号不一致。

下面的数据框:

    Date Invoice_Number      Product_Code    Quantity
    0      20200201       INV34801                          1.00
    1      20200201       INV34802                          1.00
    2      20200201       INV34803                          1.00
    3      20200201       INV34804                          1.00
    4      20200201       INV34805                          8.00

非常感谢您的善意建议。

【问题讨论】:

  • 这是什么正则表达式\s\s\s\s\s\w\w\w\w\w\w\w\w\w\w\w|\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s|\n \s\s\s\s\s\s\s\s\s\s\s\s\d\d\d\d|\s\s\s\s\s\s\s\s\s\s\d\d\d\d\d\d你可以用\s{m,n}\w{m,n}etc
  • 这能回答你的问题吗? How to parse CSV file using pandas?

标签: python regex dataframe


【解决方案1】:

试试这个:

regex = r"[H,D] (?P<invoice_nr>[^,]*)(, (?P<date>[^,]*)[\s\S]*?(?P<quantity>-?\d+\.00), IN)[\s\S]*?(\n|$)"

我给你做了一个例子here,不确定这是否有帮助,但它应该给你一些关于正则表达式的指导

【讨论】:

    猜你喜欢
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    相关资源
    最近更新 更多