如何使用 For 循环遍历对象(保存在列表中)?
How To Iterate Through Objects (Saved in List) Using For Loop?
我正在 运行宁 API 从网站上获取一些信息,我将信息存储在列表“[]”中。我如何 运行 通过此信息进行 for 循环以:
1) 在for循环中遍历对象列表(具体比较一个对象文本
2)如果对象的一个值等于1个词,将整个对象保存到一个新列表中
我已经尝试 运行在 list/objects 中使用 for 循环,但出现错误“'list' 对象不可调用”
tree = Et.fromstring(response.content)
for child in tree.findall('meterConsumption'):
for audit in child.findall('audit'):
for creator in audit.findall('createdBy'):
for ID in child.findall('id'):
print ('Entry ID: ',ID.text)
for use in child.findall('usage'):
print ('Use: ',use.text)
for cost in child.findall('cost'):
print ('Cost: ',cost.text)
for startdate in child.findall('startDate'):
print ('Startdate: ',startdate.text)
for enddate in child.findall('endDate'):
print ('Enddate: ',enddate.text)
#save object to list
allentries.append(Entry(ID.text,
use.text,
cost.text,
startdate.text,
enddate.text,
creator.text))
for x in allentries():
print (x.entryid)
我想要获取对象中所有键值对的列表。例如:
- Id[1], use[1], cost[1], startdate[1], enddate[1], creator[1]
- Id[2], use[2], cost[2], startdate[2], enddate[2], creator[2]
- Id[3], use[3], cost[3], startdate[3], enddate[3], creator[3]
由此可见,如果创建者 == "human"。将此对象的所有信息追加到新对象列表中
当您尝试像调用函数一样调用列表时,您会遇到 'list' object is not callable
错误。这发生在你的倒数第二行:
for x in allentries():
print (x.entryid)
因为 allentries
是你的列表,所以在它的末尾添加一个 ()
是 "calling" 它的语法,这对于不是'的对象没有意义函数。所以正确的语法是:
for x in allentries:
print (x.entryid)
对于你的第二个问题,我建议研究 pandas
DataFrames 作为在 Python 中处理表格数据的简便方法。由于 child.findall()
为您提供了一个对象列表,您可以使用 .text
从中提取文本,因此您可以将列表字典传递给 DataFrame
构造函数,如下所示:
import pandas as pd
# Initialize empty dataframe
allentries = pd.DataFrame()
for child in tree.findall('meterConsumption'):
for audit in child.findall('audit'):
for creator in audit.findall('createdBy'):
# Also touched up your indentation
for ID in child.findall('id'):
print ('Entry ID: ',ID.text)
for use in child.findall('usage'):
print ('Use: ',use.text)
for cost in child.findall('cost'):
print ('Cost: ',cost.text)
for startdate in child.findall('startDate'):
print ('Startdate: ',startdate.text)
for enddate in child.findall('endDate'):
print ('Enddate: ',enddate.text)
# Use list comprehensions to extract text attributes
allentries.append({
'ID': [ID.text for ID in child.findall('id')],
'use': [use.text for use in child.findall('use')],
'cost': [cost.text for cost in child.findall('cost')],
'startdate': [startdate.text for startdate in child.findall('startDate')],
'enddate': [enddate.text for enddate in child.findall('endDate')]
})
# Display the final dataframe
print(allentries)
三重 for 循环后跟一个 for 循环 for child.findall('id') 将导致称为 identation error
.
的编译时错误
for child in tree.findall('meterConsumption'):
for audit in child.findall('audit'):
for creator in audit.findall('createdBy'):
#identation error
for ID in child.findall('id'):
print ('Entry ID: ', ID.text)
列表对象不可调用意味着您正在尝试调用列表对象。
allentries 是一个列表,你正在尝试使用 ()
调用一个列表。
删除此 ()
.
我正在 运行宁 API 从网站上获取一些信息,我将信息存储在列表“[]”中。我如何 运行 通过此信息进行 for 循环以:
1) 在for循环中遍历对象列表(具体比较一个对象文本 2)如果对象的一个值等于1个词,将整个对象保存到一个新列表中
我已经尝试 运行在 list/objects 中使用 for 循环,但出现错误“'list' 对象不可调用”
tree = Et.fromstring(response.content)
for child in tree.findall('meterConsumption'):
for audit in child.findall('audit'):
for creator in audit.findall('createdBy'):
for ID in child.findall('id'):
print ('Entry ID: ',ID.text)
for use in child.findall('usage'):
print ('Use: ',use.text)
for cost in child.findall('cost'):
print ('Cost: ',cost.text)
for startdate in child.findall('startDate'):
print ('Startdate: ',startdate.text)
for enddate in child.findall('endDate'):
print ('Enddate: ',enddate.text)
#save object to list
allentries.append(Entry(ID.text,
use.text,
cost.text,
startdate.text,
enddate.text,
creator.text))
for x in allentries():
print (x.entryid)
我想要获取对象中所有键值对的列表。例如:
- Id[1], use[1], cost[1], startdate[1], enddate[1], creator[1]
- Id[2], use[2], cost[2], startdate[2], enddate[2], creator[2]
- Id[3], use[3], cost[3], startdate[3], enddate[3], creator[3]
由此可见,如果创建者 == "human"。将此对象的所有信息追加到新对象列表中
当您尝试像调用函数一样调用列表时,您会遇到 'list' object is not callable
错误。这发生在你的倒数第二行:
for x in allentries():
print (x.entryid)
因为 allentries
是你的列表,所以在它的末尾添加一个 ()
是 "calling" 它的语法,这对于不是'的对象没有意义函数。所以正确的语法是:
for x in allentries:
print (x.entryid)
对于你的第二个问题,我建议研究 pandas
DataFrames 作为在 Python 中处理表格数据的简便方法。由于 child.findall()
为您提供了一个对象列表,您可以使用 .text
从中提取文本,因此您可以将列表字典传递给 DataFrame
构造函数,如下所示:
import pandas as pd
# Initialize empty dataframe
allentries = pd.DataFrame()
for child in tree.findall('meterConsumption'):
for audit in child.findall('audit'):
for creator in audit.findall('createdBy'):
# Also touched up your indentation
for ID in child.findall('id'):
print ('Entry ID: ',ID.text)
for use in child.findall('usage'):
print ('Use: ',use.text)
for cost in child.findall('cost'):
print ('Cost: ',cost.text)
for startdate in child.findall('startDate'):
print ('Startdate: ',startdate.text)
for enddate in child.findall('endDate'):
print ('Enddate: ',enddate.text)
# Use list comprehensions to extract text attributes
allentries.append({
'ID': [ID.text for ID in child.findall('id')],
'use': [use.text for use in child.findall('use')],
'cost': [cost.text for cost in child.findall('cost')],
'startdate': [startdate.text for startdate in child.findall('startDate')],
'enddate': [enddate.text for enddate in child.findall('endDate')]
})
# Display the final dataframe
print(allentries)
三重 for 循环后跟一个 for 循环 for child.findall('id') 将导致称为 identation error
.
for child in tree.findall('meterConsumption'):
for audit in child.findall('audit'):
for creator in audit.findall('createdBy'):
#identation error
for ID in child.findall('id'):
print ('Entry ID: ', ID.text)
列表对象不可调用意味着您正在尝试调用列表对象。
allentries 是一个列表,你正在尝试使用 ()
调用一个列表。
删除此 ()
.