1 回答
TA贡献1775条经验 获得超11个赞
你可以不使用 Pandas
s = '''survey, a, b, c
section, 1, 2, 3
observation, a, b, c
values, 1, 2, 3
values, 4, 5, 6
observation, d, e, f
values, 7, 8, 9
section, 4, 5, 6'''
list_s = s.strip().split('\n')
list_s = [x.strip() for x in list_s]
list_s
# ['survey, a, b, c', 'section, 1, 2, 3', 'observation, a, b, c', 'values, 1, 2, 3', 'values, 4, 5, 6', 'observation, d, e, f', 'values, 7, 8, 9', 'section, 4, 5, 6']
for el in list_s:
if el.split(',')[0] == 'survey':
survey = el
if el.split(',')[0] == 'section':
section = el
if el.split(',')[0] == 'observation':
observation = el
if el.split(',')[0] == 'values':
print(f"{survey},{section},{observation},{el}")
#survey, a, b, c,section, 1, 2, 3,observation, a, b, c,values, 1, 2, 3
#survey, a, b, c,section, 1, 2, 3,observation, a, b, c,values, 4, 5, 6
#survey, a, b, c,section, 1, 2, 3,observation, d, e, f,values, 7, 8, 9
添加回答
举报