简体   繁体   English

如何获取存储在变量中的对象 ID 的值 - 在 python 中

[英]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.

相关问题 如何从 azure 管道获取存储在 python 脚本中的变量的值 - how to get the value of a variable stored in a python script from azure pipeline Python:如何使用存储在变量中的值? - Python: how to use the value stored in a variable? Python:您能否命名一个 object 并将值存储在变量中? - Python: Can you name an object with a value stored in a variable? 用于Python 3.X的tkinter - 如何获取输入到字段中的文本值作为变量存储? - tkinter for Python 3.X - How to I get the value of text entered into the a field to be stored as a variable? 如何从python中的JSON对象读取属性,此属性存储在变量中 - how to read a property from JSON object in python, this property is stored in a variable Selenium / Python-通过存储的变量选择元素ID - Selenium / Python - Selecting an element id by a stored variable Python id(),带有对象的变量引用 - Python id() with variable reference to an object Python / Django-从对象变量值获取带有索引的列表值 - Python / Django - Get List Value With Index From Object Variable Value 如何获取存储在python中十六进制文件位置的值? - how to get the value stored in the hex file location in python? 如何从python脚本中的ini文件读取和获取存储值? - How to read and get the stored value from ini file in python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM