我有一个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')
我想逐行访问dataframe并在另一个csv文件中打印productid、storeid和单元号,如下所示:
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()
此代码工作正常,只是忽略了第一行(2327146,1)的编写。请建议更改
您的代码忽略了第一行,因为默认情况下
read_csv
假定它是头。您可以通过添加上面建议的header=None
使原始代码正常工作。您可能还需要考虑使用正则表达式来提取值的更可读的版本顺便说一句,熊猫并不是真正必要的。这也会起作用:
Add header=None参数
df = pd.read_csv('a.txt', header=None)
解决了使用元组读取文件时的问题
相关问题 更多 >
编程相关推荐