我有某种导入的通用索引
f = open(indexfile, "r")
结果对象是一个 _io.TextIOWrapper,如下所示:
GROUP_FIELD_NAME:ID
GROUP_FIELD_VALUE:1
GROUP_FIELD_NAME:NAME
GROUP_FIELD_VALUE:Joe
GROUP_OFFSET:0
GROUP_LENGTH:1234
GROUP_FILENAME:/tmp/something1
GROUP_FIELD_NAME:ID
GROUP_FIELD_VALUE:2
GROUP_FIELD_NAME:NAME
GROUP_FIELD_VALUE:Jenny
GROUP_OFFSET:1235
GROUP_LENGTH:12
GROUP_FILENAME:/tmp/something2
其中一些数据字段可以通过组合相应的_NAME和_VALUE来提取,而一些字段只需要查看名称(_OFFSET、_LENGTH、_FILENAME)。例如,通过循环遍历每一行并填充列表,如下所示:
Import pandas as pd
ID = []
NAME = []
GROUP_LENGTH = []
GROUP_OFFSET = []
GROUP_FILENAME = []
for line in file:
if GROUP_OFFSET then add to list
if GROUP_FIELD_NAME:ID then add GROUP_FIELD_VALUE from next line
a = {'ID': ID,
'NAME': NAME,
'GROUP_LENGTH': GROUP_LENGTH,
'GROUP_OFFSET': GROUP_OFFSET,
'GROUP_FILENAME': GROUP_FILENAME
}
df = pd.DataFrame.from_dict(a, orient='index')
df = df.transpose()
我怎样才能得到这样的东西:
ID NAME GROUP_LENGTH GROUP_OFFSET GROUP_FILENAME
1 Joe 1234 0 /tmp/something1
2 Jenny 12 1235 /tmp/something2
MMTTMM
慕村225694
相关分类