简体   繁体   中英

Splitting string in Python with multiple occurrence of split keyword

So, I wanna split filter and update parameter for MongoDB replace one method available in Pymongo Library.

{filter}, {update}

are passed to me from a file, one pair per line

Eg: {"k1":"v1"}, {"k1":"v1", "k2":"v2", "k3":"v3"}

What I wanna do ?

Split them such that I get two dict variables,

filter = {"k1":"v1"}
update = {"k1":"v1", "k2":"v2", "k3":"v3"}

What have I tried ?

Problem is, I don't wanna change the original format and if I split them using "," then it might split abruptly, I can't also rely on splitting on first occurrence of "," as filter part itself might have multiple ","

def data_replace_one(host, port, dbname, coll_name, file_path, authdb):

    if LOCALHOST:
        client = pymongo.MongoClient(host, port)       
    else:
        print("Enter credentials:")
        uname = input("Username: ")
        pwd = getpass.getpass()

        client = pymongo.MongoClient(
            host, port, username=uname, password=pwd, authSource=authdb)

    db = client[dbname]
    coll = db[coll_name]

    with open(file_path) as in_file:
        list_dict_queries = [line.strip() for line in in_file]

    list_dict_queries = list(filter(None, list_dict_queries))

    for query in list_dict_queries:
        query_list = query.split("|")
        query_list[0] = query_list[0].strip()
        query_list[1] = query_list[1].strip()
        #print(literal_eval(query_list[0]), literal_eval(query_list[1]))
        coll.replace_one(literal_eval(
            query_list[0]), literal_eval(query_list[1]))

I think it would be simplest to add some square braces around each line, and then interpret it as JSON - assuming that your input format is guaranteed to be JSON compliant.

Something like:

import json

with open(file_path) as in_file:
    list_dict_queries = [('[' + line.strip() + ']') for line in in_file]

query_list = [json.loads(n) for n in list_dict_queries]

If you would not have braces/curly brackets anywhere else, then you can use the following.

>>> filter, update = re.findall('{.*?}', '{"k1":"v1"}, {"k1":"v1", "k2":"v2", "k3":"v3"}')

>>> filter
'{"k1":"v1"}'

>>> update
'{"k1":"v1", "k2":"v2", "k3":"v3"}'

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