[英]How to get the value of an object ID stored in a variable - in python
I have a schoolID variable that is an Object.我有一个 schoolID 变量,它是一个对象。 This is what prints out to the console when I print it:
这是我打印时打印到控制台的内容:
school._id
Out[60]:
0 5ca588639d83cb7261b73231
Name: _id, dtype: object
However, in order for my function to work, I need it to be a string.但是,为了让我的函数工作,我需要它是一个字符串。 So at the moment I am having to create another variable and type in the string value myself.
所以目前我不得不创建另一个变量并自己输入字符串值。 Below is my function currently:
以下是我目前的功能:
fun = '5ca588639d83cb7261b73231'
update_this = Example.query.filter_by(_id=fun).first()
I want to be able to to use the schoolID variable in the function like this:我希望能够像这样在函数中使用 schoolID 变量:
update_this = Example.query.filter_by(_id=schoolID).first()
I have tried converting the object to a string using the str function, but that creates the whole object as a string, rather than just the ID number that I need, see below:我曾尝试使用 str 函数将对象转换为字符串,但这会将整个对象创建为字符串,而不仅仅是我需要的 ID 号,请参见下文:
str(school._id)
Out[59]: '0 5ca588639d83cb7261b73231\nName: _id, dtype: object'
Does anyone know how I can get just the '5ca588639d83cb7261b73231' part of the object saved into a variable so I can use it in my function?有谁知道我如何才能将对象的“5ca588639d83cb7261b73231”部分保存到变量中,以便我可以在我的函数中使用它?
school._id
is a series, hence your problem. school._id
是一个系列,因此你的问题。 You can quickly fix by school._id.iloc[0]
if you are sure the required id is the first one in the series.如果您确定所需的 id 是系列中的第一个,您可以通过
school._id.iloc[0]
快速修复。 Consider keeping school._id
as a str
instead of pd.Series
.考虑将
school._id
作为str
而不是pd.Series
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.