读取数据帧的困难

我有一个 csv 文件,如下所示:


[8:3:1978] LOG [Sale:internals.py:makeSaleEntry:0] Entered with productid= 2327, storeid= 146, No.OfUnits= 1

[19:1:2007] LOG [Sale:internals.py:makeSaleEntry:1] Entered with productid= 1908, storeid= 202, No.OfUnits= 11

[22:4:2001] LOG [Sale:internals.py:makeSaleEntry:2] Entered with productid= 3072, storeid= 185, No.OfUnits= 16

[22:12:1915] LOG [Sale:internals.py:makeSaleEntry:3] Entered with productid= 1355, storeid= 177, No.OfUnits= 1

[19:8:1963] LOG [Sale:internals.py:makeSaleEntry:4] Entered with productid= 2235, storeid= 35, No.OfUnits= 16

[16:11:1997] LOG [Sale:internals.py:makeSaleEntry:5] Entered with productid= 1439, storeid= 141, No.OfUnits= 26

我已经使用数据帧 df 读取了该文件。


df = pd.read_csv('a.txt')

我想逐行访问数据框并在另一个 csv 文件中打印 productid、storeid 和 No.OfUnits,如下所示:


2327,146,1

1908,202,11

3072,185,16

1355,177,1

2235,35,16

1439,141,26

我的一段代码是:


for i, row in df.iterrows():

    strr = ""

    for j, column in row.iteritems():

        seq = column.split('= ')

        strr = strr + seq[1] + ","

    file = open("a.csv", "a")

    file.write(strr[:-1]+"\n")

    file.close()

这段代码工作正常,只是它忽略了写第一行 (2327,146,1)。请提出更改建议。


至尊宝的传说
浏览 154回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python