如何用python中其他csv文件的值替换csv的一个值?

时间:2019-06-18 10:33:29

标签: python csv

我有两个文件bmg1.csv和bmg2.csv

bmg1.csv

layout_width

bmg2.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)"

现在我要用bmg1.csv的Bloomberg数据替换bmg2.csv(在mktdata /之后)中的每个对应的Bloomberg数据

例如“ BBG00J2FF9V2 Equity”(在bmg2.csv中)和“ BBG000BLNNH6 Equity”(来自bmg1.csv)。

我尝试了下面的代码,但不知道如何继续进行。如果有人知道答案,请提供帮助。

logic.py

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}}"}

所需的输出

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)

1 个答案:

答案 0 :(得分:2)

您可以使用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))

输出:

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}}}