【发布时间】:2021-03-29 07:49:50
【问题描述】:
文本文件中的示例数据
[User]
employeeNo=123
last_name=Toole
first_name=Michael
language=english
email = michael.toole@123.ie
department=Marketing
role=Marketing Lead
[User]
employeeNo=456
last_name= Ronaldo
first_name=Juan
language=Spanish
email=juan.ronaldo@sms.ie
department=Data Science
role=Team Lead
Location=Spain
[User]
employeeNo=998
last_name=Lee
first_name=Damian
language=english
email=damian.lee@email.com
[User]
想知道是否有人可以帮助我,您可以在上面查看我的示例数据集。我想做的(请告诉我是否有更有效的方法)是循环遍历第一列,并且无论出现唯一 ID 列表的任何位置(例如 first_name、last_name、role 等)都将相应行中的值附加到该列表并执行每个唯一 ID 的操作,以便我留下以下内容。 我读过关于多索引的文章,我不确定这是否是一个更好的解决方案,但我无法让它工作(我对 python 很陌生)
# Define a list of selected persons
selectedList = textFile
# Define a list of searching person
searchList = ['uid']
# Define an empty list
foundList = []
# Iterate each element from the selected list
for index, sList in enumerate(textFile):
# Match the element with the element of searchList
if sList in searchList:
# Store the value in foundList if the match is found
foundList.append(selectedList[index])
【问题讨论】:
-
您显示的不是文本文件,而是电子表格的图像。我无法从中猜出 TEXT 文件的格式,因此我无法为您提供帮助。请将文件内容显示为可复制的文本并在问题本身中显示。
-
添加了示例数据文本文件。
标签: python dataframe jupyter-notebook