Python Dict解析问题



我的代码创建了一个dict文件Sam10.txt:

我的代码:

res=helpers.scan(es,query=search_param,index=index_name, size=10000,request_timeout = None,scroll= '10m')
x=0
#i=1
with open('Sam10.txt', 'w') as f:
for i in res:
x=x+1
my_dict = i['_source']
w = csv.DictWriter(f,my_dict.keys())
w.writerow(my_dict)
if x==3:
exit()

该文件的输出如下所示:仅显示一行

"{'messageId': 'wssfx_20181012213423_8945_1_000011326', 'businessId': '2018081310342', 'batchId': 'wssfx_20181012213423_8945_1', 'sourceSystem': 'wssfx', 'secondarySourceSystem': None, 'sourceSystemCreationTimestamp': '2018-10-13T01:36:31.217Z', 'sentBy': 'wssfx', 'sentTo': 'SA', 'messageType': 'Trade', 'schemaVersion': '1.3', 'processing': 'EOD'}","{'tradeHeader': {'scotiaUPI': None, 'assetClass': 'NonCash-ForeignExchange-Forward', 'algoProductCategory': 'FX-Forex-SI', 'isInternalDeal': False, 'isTradingBook': True, 'tradeDate': '2018-08-13', 'entryDateTime': '2018-08-13T16:00:04.000Z', 'executionDateTime': {'millisTimestamp': '2018-08-13T16:00:04.000Z', 'nanoOfSecond': 0}, 'originalExecutionDateTime': None, 'tradeUpdateDateTime': '2018-08-13T16:00:04.000Z', 'tradeStatus': 'LIVE', 'tradeEvent': 'SnapShot', 'tradeSubEvent': None, 'sourceSystemProductId': {'sourceInternalId': 'FXF', 'sourceInternalIdType': None, 'sourceInstrumentName': 'FX FORWARD TRANSACTION', 'sourceInstrumentCategory': 'FX', 'sourceIsin': '', 'sourceCusip': None, 'sourceSedol1': None, 'sourceSedol2': None, 'primaryMarketId': None, 'primaryMarketIdType': None}, 'csaEligible': True, 'isPartOfPortfolioCompression': None, 'tradeIdentifiers': {'tradeId': {'id': '2018081310342', 'version': None}, 'previousTradeId': None, 'originatingTradeId': {'id': '2018081310342', 'version': None}, 'originatingOrderId': '', 'originatingParentOrderId': None, 'originatingQuoteId': None, 'originatingParentQuoteId': None, 'venueTransactionId': '', 'uniqueSwapId': '', 'uniqueTransactionId': None, 'tradePackageId': None, 'tradePackageSize': None, 'internalReverseTradeId': None, 'tradeName': None}, 'venueInfo': {'executionVenueType': None, 'executionPlatformId': '', 'exchangeCode': '', 'exchangeCodeType': None}, 'persons': {'tradeExecutorId': 'SML', 'traderId': None, 'traderName': None, 'traderLocation': None, 'salesPersonId': 'SML', 'salesPersonName': 'Colin Smolders', 'salesPersonLocation': None, 'algorithmId': None, 'algorithmName': None, 'algorithmLocation': None}, 'settlement': {'settlementType': 'Physical', 'isClearingEligible': False, 'isNetted': None, 'settlementDate': '2019-10-31'}, 'regulatory': {'isdaUPIv1': 'ForeignExchange:Forward', 'isdaUPIv2': None, 'isdaAssetClass': 'ForeignExchange', 'isdaBaseProductId': 'Forward', 'isdaSubProductId': None, 'isdaTransactionType': None, 'cfiCode': None, 'isDoddFrankUsPerson': None, 'isVolckerSpot': None, 'isEmirSpot': False, 'mifidTradingCapacity': None, 'mifidTradingCapacityEnum': 'DEAL', 'mifidInvestmentDecisionWithinFirm': 'SML', 'mifidExecutionWithinFirm': 'SML', 'isMifidRTO': None, 'isMifidPriceMaker': False, 'isHedgeTrade': False, 'isMifidAgencyTrade': False, 'isMifidSecuritiesFinancingTrans': False, 'isMifidCommodityDerivative': None, 'mifidTransparencyFlag': None, 'mifidWaiverIndicators': None, 'mifidOtcPostTradeIndicators': None, 'mifidInstrumentIdentificationType': None, 'mifidInstrumentIdentificationCode': '', 'isMifid2FinInstrument': None, 'mifidLastLqdtyInd': None, 'mifidBuySell': 'Buy', 'mifidQuantity': 1509000.0, 'mifidQuantityCurrency': 'USD', 'mifidPriceCurrency': 'CAD', 'mifidNotionalCurrencyAmount': 1509000.0, 'mifidOutstandingNotionalAmount': None, 'mifidNotionalCurrency': 'USD', 'mifidNotionalCurrency1': 'CAD', 'mifidNotionalCurrency2': 'USD', 'mifidOtherDetails': None, 'shortSellingIndicator': None}}, 'book': {'bookingPoint': None, 'bookId': 'NAFXFW', 'bookDescription': 'NORTH AMERICAN FORWARD', 'scotiaLegalEntityId': 'L3I9ZG2KFGXZ61BMYR72', 'transitNumber': '67496'}, 'parties': {'counterparty': {'partyId': 'TDBT', 'partyIdType': None, 'partyName': 'TD BANK TREASURY', 'partyLei': 'PT3QB789TSUIDF371261', 'cardsId': None, 'ccdId': None}, 'originalCounterparty': {'partyId': 'TDBT', 'partyIdType': None, 'partyName': 'TD BANK TREASURY', 'partyLei': 'PT3QB789TSUIDF371261', 'cardsId': None, 'ccdId': None}, 'client': None, 'cardsId': '086641', 'ccdId': '1000177', 'executingParty': {'partyId': 'GFXGL', 'partyIdType': None, 'partyName': 'GFXGL', 'partyLei': 'L3I9ZG2KFGXZ61BMYR72', 'cardsId': None, 'ccdId': None}, 'executingBroker': None, 'clearingParty': {'partyId': None, 'partyIdType': None, 'partyName': None, 'partyLei': None, 'cardsId': None, 'ccdId': None}, 'orderOriginatingParty': None, 'triPartyAgent': None}, 'costsAndCharges': None, 'clearingInstructions': None, 'sourceSystemSpecific': None, 'product': {'npvCurrency': 'USD', 'payCurrency': 'USD', 'payNotional': 1509000.0, 'payDiscountCurve': 'DK', 'receiveCurrency': 'CAD', 'receiveNotional': 1969607.16, 'receiveDiscountCurve': 'DK', 'paymentHolidayCenters': ['CAD', 'USD'], 'theoreticalModel': '', 'fxRate': {'currencyPair': 'USDCAD', 'quoteTimestamp': '2018-08-13T16:00:04.000Z', 'quoteBasis': 'ReceiveCurrencyPerPayCurrency', 'quoteValue': 1.30524}, 'isSpotTrade': False, 'isForwardStarting': None, 'calculatedTrueSpotDate': '2018-08-14', 'isPaySideNonDeliverable': None, 'payDeliveryCurrency': 'USD', 'payNdfReferenceIndex': None, 'payNdfResetOffset': None, 'payNdfFxReset': None, 'isReceiveSideNonDeliverable': None, 'receiveDeliveryCurrency': 'CAD', 'receiveNdfReferenceIndex': None, 'receiveNdfResetOffset': None, 'receiveNdfFxReset': None, 'ndfResetHolidayCenters': None, 'isTimeOptionForward': False, 'timeOptionSet': None, 'isPartOfFxSwap': False, 'fxSwapSet': None, 'events': {'effectiveDate': '2018-08-13', 'terminationDate': '2019-10-31', 'tenorBusinessPeriod': {'periodMultiplier': 301, 'period': 'd'}, 'contractBreaks': None}}}"

我的问题是。

如何从这句话中提取的值。

我一直得到以下错误:

for lr in sam_dict['header']:
TypeError: 'generator' object is not subscriptable

TypeError: string indices must be integers

最终输出应该是csv文件。我们只举3个字段为例:

messageId,scotiaUPI,sourceInternalId
wssfx_20181012213423_8945_1_000011326,None,FXF

感谢您的帮助。

谨致问候,Sam

感谢您的回复。

我能够通过解析它

使用读取文件

f=open('XXXX.txt','r')
lineread=f.readlines()  # f.read() does not work for some reason
for i in lineread:
print(eval(i))   #just the eval() function

因此,通过使用以下方法解决了问题:f.readlines((而不是f.read((和eval((

最新更新