如何从 python 的输出中获取特定部分

How to get a specific part from my output of python

我一直在尝试获取输出中的特定部分,但无法做到。

我使用的代码片段如下:

from nsetools import Nse
nse = Nse()
print (nse)
q1 = nse.get_quote('canbk') # it's ok to use both upper or lower case for codes.
from pprint import pprint # just for neatness of display
pprint(q1)
q2 = nse.get_quote('sbin') # it's ok to use both upper or lower case for codes.
from pprint import pprint # just for neatness of display
pprint(q2)


对于上面的代码,我得到如下输出:

Driver Class for National Stock Exchange (NSE)
{'adhocMargin': 22.84,
 'applicableMargin': 48.41,
 'averagePrice': 119.36,
 'basePrice': 127.4,
 'bcEndDate': '10-AUG-20',
 'bcStartDate': '04-AUG-20',
 'buyPrice1': 119.0,
 'buyPrice2': 118.95,
 'buyPrice3': 118.9,
 'buyPrice4': 118.85,
 'buyPrice5': 118.8,
 'buyQuantity1': 1873.0,
 'buyQuantity2': 1652.0,
 'buyQuantity3': 4873.0,
 'buyQuantity4': 3643.0,
 'buyQuantity5': 26144.0,
 'change': '-8.40',
 'closePrice': 0.0,
 'cm_adj_high_dt': '23-DEC-19',
 'cm_adj_low_dt': '24-MAR-20',
 'cm_ffm': 3888.7,
 'companyName': 'Canara Bank',
 'css_status_desc': 'Listed',
 'dayHigh': 123.5,
 'dayLow': 117.3,
 'deliveryQuantity': 2719456.0,
 'deliveryToTradedQuantity': 24.44,
 'exDate': '31-JUL-20',
 'extremeLossMargin': 3.5,
 'faceValue': 10.0,
 'high52': 232.85,
 'indexVar': None,
 'isExDateFlag': False,
 'isinCode': 'INE476A01014',
 'lastPrice': 119.0,
 'low52': 73.65,
 'marketType': 'N',
 'ndEndDate': None,
 'ndStartDate': None,
 'open': 123.5,
 'pChange': '-6.59',
 'previousClose': 127.4,
 'priceBand': 'No Band',
 'pricebandlower': 114.7,
 'pricebandupper': 140.1,
 'purpose': 'ANNUAL GENERAL MEETING',
 'quantityTraded': 11127677.0,
 'recordDate': None,
 'secDate': '14-Dec-2020 00:00:00',
 'securityVar': 22.07,
 'sellPrice1': 119.1,
 'sellPrice2': 119.15,
 'sellPrice3': 119.2,
 'sellPrice4': 119.25,
 'sellPrice5': 119.3,
 'sellQuantity1': 6052.0,
 'sellQuantity2': 2541.0,
 'sellQuantity3': 5136.0,
 'sellQuantity4': 4241.0,
 'sellQuantity5': 11682.0,
 'series': 'EQ',
 'surv_indicator': None,
 'symbol': 'CANBK',
 'totalBuyQuantity': 1345751.0,
 'totalSellQuantity': 2526943.0,
 'totalTradedValue': 22252.34,
 'totalTradedVolume': 18643043.0,
 'varMargin': 22.07}
{'adhocMargin': 13.75,
 'applicableMargin': 35.12,
 'averagePrice': 271.69,
 'basePrice': 274.2,
 'bcEndDate': '28-JUN-18',
 'bcStartDate': '19-JUN-18',
 'buyPrice1': 271.25,
 'buyPrice2': 271.2,
 'buyPrice3': 271.15,
 'buyPrice4': 271.1,
 'buyPrice5': 271.05,
 'buyQuantity1': 1682.0,
 'buyQuantity2': 4551.0,
 'buyQuantity3': 12238.0,
 'buyQuantity4': 41281.0,
 'buyQuantity5': 18363.0,
 'change': '-2.90',
 'closePrice': 0.0,
 'cm_adj_high_dt': '02-JAN-20',
 'cm_adj_low_dt': '22-MAY-20',
 'cm_ffm': 105226.52,
 'companyName': 'State Bank of India',
 'css_status_desc': 'Listed',
 'dayHigh': 274.05,
 'dayLow': 270.8,
 'deliveryQuantity': 8886488.0,
 'deliveryToTradedQuantity': 27.82,
 'exDate': '15-JUN-18',
 'extremeLossMargin': 3.5,
 'faceValue': 1.0,
 'high52': 339.85,
 'indexVar': None,
 'isExDateFlag': False,
 'isinCode': 'INE062A01020',
 'lastPrice': 271.3,
 'low52': 149.45,
 'marketType': 'N',
 'ndEndDate': None,
 'ndStartDate': None,
 'open': 274.05,
 'pChange': '-1.06',
 'previousClose': 274.2,
 'priceBand': 'No Band',
 'pricebandlower': 246.8,
 'pricebandupper': 301.6,
 'purpose': 'ANNUAL GENERAL MEETING/ CHANGE IN REGISTRAR AND TRANSFER AGENT',
 'quantityTraded': 31946501.0,
 'recordDate': None,
 'secDate': '14-Dec-2020 00:00:00',
 'securityVar': 17.87,
 'sellPrice1': 271.3,
 'sellPrice2': 271.35,
 'sellPrice3': 271.4,
 'sellPrice4': 271.45,
 'sellPrice5': 271.5,
 'sellQuantity1': 3329.0,
 'sellQuantity2': 5708.0,
 'sellQuantity3': 10295.0,
 'sellQuantity4': 17472.0,
 'sellQuantity5': 22788.0,
 'series': 'EQ',
 'surv_indicator': None,
 'symbol': 'SBIN',
 'totalBuyQuantity': 1559642.0,
 'totalSellQuantity': 2361766.0,
 'totalTradedValue': 12831.24,
 'totalTradedVolume': 4722752.0,
 'varMargin': 17.87}

我只要

'closePrice': 127.4, 
'open': 126.95,
'pChange': 1.6,
'symbol': 'xxxx',

然后我想比较if closeprice>open AND pCHange>1.5如果满足条件then

相同的对应输出应如下所示:

****** BUY **SYMBol name*********

谁能帮我做同样的事情?提前致谢。

您从函数调用中获得的 return 值是一个字典。因此,您可以简单地遍历所有元素并使用以下代码打印它们的相应属性:

from nsetools import Nse
nse = Nse()
print (nse)
q1 = nse.get_quote('canbk') # it's ok to use both upper or lower case for codes.
from pprint import pprint # just for neatness of display
# pprint(q1)
q2 = nse.get_quote('sbin') # it's ok to use both upper or lower case for codes.
from pprint import pprint # just for neatness of display
pprint(q2)

# Loop to print specific attributes
for i in range(len(q2)):

    print("Printing for the item : ", i+1)
    print("closePrice : ", q2["closePrice"])
    print("open :", q2["open"])
    print("pChange : ", q2["pChange"])
    print("symbol : ", q2["symbol"])
    print()

    
    if(q2["closePrice"] > q2["open"] and q2["pCHange"]>1.5):
        print("BUY", q2["symbol"])