简体   繁体   English

仅打印对象的一部分

[英]Printing only one part of an object

Using Python 3.8.0, this code使用 Python 3.8.0,这段代码

p = get_quote_yahoo("AUPH")
print(p) 

gives result:给出结果:

"language region quoteType  ...     market esgPopulated   price
AUPH    en-US     US    EQUITY  ...  us_market        False  19.825
[1 rows x 60 columns]"

and this code:和这个代码:

print(p.price)

gives:给出:

"AUPH    19.8893
 Name: price, dtype: float64"

How do I access only the floating number ( 19.8893 ) in p so that only the number prints?如何仅访问p的浮点数 ( 19.8893 ) 以便仅打印数字?

尝试这个:

print(p.price.values[0])

Since p.price is a Series object you can use all the methods available.由于p.price是一个Series 对象,因此您可以使用所有可用的方法。

This is the correct way to get the value这是获取值的正确方法

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(next(iter(p.price)))

Or或者

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(p.price.get(0))

Or或者

import pandas_datareader as pdr

p = pdr.get_quote_yahoo('AUPH')

print(p.price[0])

Outputs输出

# > python test.py
20.01

Let assume you have this string:假设你有这个字符串:

m = """AUPH    19.8893
    Name: price, dtype: float64"""

If you use split method you will have:如果您使用split方法,您将拥有:

>>> m.split()
['AUPH', '19.8893', 'Name:', 'price,', 'dtype:', 'float64']
>>> m.split()[1]
'19.8893'
>>> float(m.split()[1])
19.8893

So, for your case, You can get the number by doing:因此,对于您的情况,您可以通过以下方式获取号码:

m = p.price
result = float(m.split()[1])
print(result) # will display 19.8893

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM