I am trying to write below two separate dict values with the same key header to the csv file using python.
{'instrumentType': 'Stock Futures', 'expiryDate': '25-Jan-2023', 'optionType': '-', 'strikePrice': 0, 'identifier': 'FUTSTKTATASTEEL25-01-2023XX0.00', 'openPrice': 119.85, 'highPrice': 120.9, 'lowPrice': 118.3, 'closePrice': 119, 'prevClose': 119.8, 'lastPrice': 119.3, 'change': -0.5, 'pChange': -0.41736227045075125, 'numberOfContractsTraded': 13024, 'totalTurnover': 85557.26}
{'instrumentType': 'Stock Options', 'expiryDate': '25-Jan-2023', 'optionType': 'Call', 'strikePrice': 120, 'identifier': 'OPTSTKTATASTEEL25-01-2023CE120.00', 'openPrice': 4.1, 'highPrice': 4.7, 'lowPrice': 3.25, 'closePrice': 3.5, 'prevClose': 4.1, 'lastPrice': 3.55, 'change': -0.5499999999999998, 'pChange': -13.414634146341461, 'numberOfContractsTraded': 11663, 'totalTurnover': 2501.71}
I tried following the approach, but only one dict is getting stored in csv.
data = pd.DataFrame.from_dict(ce_dt)
data.to_csv('out.csv', index=False)
Appreciate your help!!
EDIT
The data is stored in a variable as follows:
There is no comma , between the dict and that's why it is getting written only one dict.
Sample:
{'underlying': 'MARUTI', 'identifier': 'OPTSTKMARUTI25-01-2023CE8500.00', 'instrumentType': 'OPTSTK', 'instrument': 'Stock Options', 'contract': 'MARUTI 25-Jan-2023', 'expiryDate': '25-Jan-2023', 'optionType': 'Call', 'strikePrice': 8500, 'lastPrice': 143, 'change': 13.55, 'pChange': 10.47, 'volume': 1870200, 'totalTurnover': 306581886, 'value': 306581886, 'premiumTurnOver': 16203281886, 'underlyingValue': 8419, 'openInterest': 2798, 'noOfTrades': 18702, 'meta': {'symbol': 'MARUTI', 'companyName': 'Maruti Suzuki India Limited', 'industry': 'AUTOMOBILES - 4 WHEELERS', 'activeSeries': ['EQ'], 'debtSeries': [], 'tempSuspendedSeries': ['IL'], 'isFNOSec': True, 'isCASec': False, 'isSLBSec': True, 'isDebtSec': False, 'isSuspended': False, 'isETFSec': False, 'isDelisted': False, 'isin': 'INE585B01010'}}
{'underlying': 'RELIANCE', 'identifier': 'OPTSTKRELIANCE25-01-2023CE2600.00', 'instrumentType': 'OPTSTK', 'instrument': 'Stock Options', 'contract': 'RELIANCE 25-Jan-2023', 'expiryDate': '25-Jan-2023', 'optionType': 'Call', 'strikePrice': 2600, 'lastPrice': 25.95, 'change': -14.3, 'pChange': -35.53, 'volume': 4438500, 'totalTurnover': 130269975, 'value': 130269975, 'premiumTurnOver': 11670369975, 'underlyingValue': 2516.65, 'openInterest': 15109, 'noOfTrades': 17754, 'meta': {'symbol': 'RELIANCE', 'companyName': 'Reliance Industries Limited', 'industry': 'REFINERIES', 'activeSeries': ['EQ'], 'debtSeries': [], 'tempSuspendedSeries': [], 'isFNOSec': True, 'isCASec': False, 'isSLBSec': True, 'isDebtSec': False, 'isSuspended': False, 'isETFSec': False, 'isDelisted': False, 'isin': 'INE002A01018'}}