简体   繁体   中英

How to replace one value of csv with value of other csv files in python?

I have two files bmg1.csv and bmg2.csv

bmg1.csv

"FX Rate","BloombergIdentifier","Strategy","PM Team"
"1","BBG000BLNNH6 Equity","QT.SCALI","DELTA ONE (SCALI)"
"1","BBG000BW3M86 Equity","QUANTITATIVE","HF EQUITY (LIN)"
"1","87157BAA1 Corp","CONVERTS","CONVERTS (ZHENG)"

bmg2.csv

CreateTime:1557770980235    {"schema":{"type":"string","optional":false},"payload":"{\"subscriptionId\":\"//blp/mktdata/BBG00J2FF9V2 Equity?fields=LAST_PRICE\",\"MarketDataEvents\":{\"LAST_PRICE\":159.2}}"}
CreateTime:1557770980473    {"schema":{"type":"string","optional":false},"payload":"{\"subscriptionId\":\"//blp/mktdata/BBG0059JSF49 Equity?fields=LAST_PRICE\",\"MarketDataEvents\":{\"LAST_PRICE\":9.38}}"}
CreateTime:1557770980541    {"schema":{"type":"string","optional":false},"payload":"{\"subscriptionId\":\"//blp/mktdata/BBG0084BBZY6 Equity?fields=LAST_PRICE\",\"MarketDataEvents\":{\"LAST_PRICE\":49.99}}"}

Now I want to replace every corresponding bloomberg data in bmg2.csv(after mktdata/) with bloomberg data of bmg1.csv

for eg "BBG00J2FF9V2 Equity" (in bmg2.csv) with "BBG000BLNNH6 Equity"(from bmg1.csv).

I tried below code but don't know, how to proceed further. Please help, if someone know the answer.

logic.py

import csv

with open('bmg1.csv', 'r') as b1:
with open('bmg2.csv', 'r') as b2:

    reader1 = csv.reader(b1, delimiter='')
    reader2 = csv.reader(b2, delimiter='mktdata/')

    both = []
    fields = reader1.next() # read header row
    reader2.next() # read and ignore header row
    for row1, row2 in zip(reader1, reader2):
        row2.append(row1[-1])
        both.append(row2)

    with open('output.csv', 'w') as output:
        writer = csv.writer(output, delimiter=',')
        writer.writerow(fields) # write a header row
        writer.writerows(both)

desired output

CreateTime:1557770979597    {"schema":{"type":"string","optional":false},"payload":"{\"subscriptionId\":\"//blp/mktdata/BBG000BLNNH6 Equity?fields=LAST_PRICE\",\"MarketDataEvents\":{\"LAST_PRICE\":159.2}}"}
CreateTime:1557770979623    {"schema":{"type":"string","optional":false},"payload":"{\"subscriptionId\":\"//blp/mktdata/BBG000BW3M86 Equity?fields=LAST_PRICE\",\"MarketDataEvents\":{\"LAST_PRICE\":159.2}}"}
CreateTime:1557770979623    {"schema":{"type":"string","optional":false},"payload":"{\"subscriptionId\":\"//blp/mktdata/87157BAA1 Corp?fields=LAST_PRICE\",\"MarketDataEvents\":{\"LAST_PRICE\":49.99}}"}

You can use zip :

import csv, re, json
_, *rate = [i[1] for i in csv.reader(open('bmg1.csv'))]
d = [(lambda x:[x[0], json.loads(x[-1])])(re.split('\s{2,}', i.strip('\n'))) for i in open('bmg2.csv')]
final_data = [[a, {**b, 'payload':(lambda x:{**x, 'subscriptionId':re.sub('(?<=mktdata/)[\w\s]+(?=\?)', j, x['subscriptionId'])})(json.loads(b['payload']))}] for j, [a, b] in zip(rate, d)]

with open('bmg_2.csv', 'w') as f:
  f.write('\n'.join(f'{a}   {json.dumps(b)}' for a, b in final_data))

Output:

CreateTime:1557770980235   {"schema": {"type": "string", "optional": false}, "payload": {"subscriptionId": "//blp/mktdata/BBG000BLNNH6 Equity?fields=LAST_PRICE", "MarketDataEvents": {"LAST_PRICE": 159.2}}}
CreateTime:1557770980473   {"schema": {"type": "string", "optional": false}, "payload": {"subscriptionId": "//blp/mktdata/BBG000BW3M86 Equity?fields=LAST_PRICE", "MarketDataEvents": {"LAST_PRICE": 9.38}}}
CreateTime:1557770980541   {"schema": {"type": "string", "optional": false}, "payload": {"subscriptionId": "//blp/mktdata/87157BAA1 Corp?fields=LAST_PRICE", "MarketDataEvents": {"LAST_PRICE": 49.99}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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