将列表的索引连接到字符串变量中
Concatenate index of a list into a string variables
我有一个列表
list = ['1a-b2', '2j-u3', '5k-hy', '1h-j3']
我有如下字符串
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"2y-9k"}'
如何用主列表中相应的索引替换字符串中的 id
值?
例如,
我想用 list
中的索引替换 main
字符串中的“1h-j3”。这也将在循环中为所有人完成。
我尝试过使用 +、% 进行连接,但它们没有用,请帮助我解决这个问题。列表索引和主变量的数据类型都是字符串
预期输出如下
在第一个循环中
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"1a-b2"}'
在第二个循环中
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"2j-u3"}'
在第三个循环中
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"5k-hy"}'
等等
好吧,根据 main
变量的数据类型,我可以想到两种方法。见下文。
以防万一,这个值是合适的JSON
import json
items_list = ['1a-b2', '2j-u3', '5k-hy', "1h-j3"]
# if main_dict was a valid json
main_dict = json.loads('{"datatype": "null", "country_code":"eu","offset":0,"id":"1h-j3"}')
main_dict["id"] = items_list.index(main_dict["id"])
main_dict = json.dumps(main_dict)
其他情况,它是一个脏字符串操作。可能有更好的方法,
# If its not a valid JSON
str_main = '{"datatype: null" "country_code":"eu","offset":0,"id":"1h-j3"}'
import re
# Use a regex to find the key for replacement.
found = re.findall(r'"id":".*"', str_main, re.IGNORECASE)
if found and len(found) > 0:
key = found[0].split(":")[1].replace('"', '')
_id = items_list.index(key)
str_main = str_main.replace(key, str(_id))
print(str_main)
产生的输出
{"datatype: null" "country_code":"eu","offset":0,"id":"3"}
--更新--
根据您的问题更新的要求,我假设这将是一个简单的循环。
items_list = ['1a-b2', '2j-u3', '5k-hy', "1h-j3"]
base_str = '{"datatype: null" "country_code":"eu","offset":0,"id":"_ID_"}'
for item in items_list:
main = base_str.replace('_ID_', item)
print(main)
产生类似
的输出
{"datatype: null" "country_code":"eu","offset":0,"id":"1a-b2"}
{"datatype: null" "country_code":"eu","offset":0,"id":"2j-u3"}
{"datatype: null" "country_code":"eu","offset":0,"id":"5k-hy"}
{"datatype: null" "country_code":"eu","offset":0,"id":"1h-j3"}
我有一个列表
list = ['1a-b2', '2j-u3', '5k-hy', '1h-j3']
我有如下字符串
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"2y-9k"}'
如何用主列表中相应的索引替换字符串中的 id
值?
例如,
我想用 list
中的索引替换 main
字符串中的“1h-j3”。这也将在循环中为所有人完成。
我尝试过使用 +、% 进行连接,但它们没有用,请帮助我解决这个问题。列表索引和主变量的数据类型都是字符串
预期输出如下 在第一个循环中
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"1a-b2"}'
在第二个循环中
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"2j-u3"}'
在第三个循环中
main = '{"datatype: null" "country_code":"eu","offset":0,"id":"5k-hy"}'
等等
好吧,根据 main
变量的数据类型,我可以想到两种方法。见下文。
以防万一,这个值是合适的JSON
import json
items_list = ['1a-b2', '2j-u3', '5k-hy', "1h-j3"]
# if main_dict was a valid json
main_dict = json.loads('{"datatype": "null", "country_code":"eu","offset":0,"id":"1h-j3"}')
main_dict["id"] = items_list.index(main_dict["id"])
main_dict = json.dumps(main_dict)
其他情况,它是一个脏字符串操作。可能有更好的方法,
# If its not a valid JSON
str_main = '{"datatype: null" "country_code":"eu","offset":0,"id":"1h-j3"}'
import re
# Use a regex to find the key for replacement.
found = re.findall(r'"id":".*"', str_main, re.IGNORECASE)
if found and len(found) > 0:
key = found[0].split(":")[1].replace('"', '')
_id = items_list.index(key)
str_main = str_main.replace(key, str(_id))
print(str_main)
产生的输出
{"datatype: null" "country_code":"eu","offset":0,"id":"3"}
--更新--
根据您的问题更新的要求,我假设这将是一个简单的循环。
items_list = ['1a-b2', '2j-u3', '5k-hy', "1h-j3"]
base_str = '{"datatype: null" "country_code":"eu","offset":0,"id":"_ID_"}'
for item in items_list:
main = base_str.replace('_ID_', item)
print(main)
产生类似
的输出{"datatype: null" "country_code":"eu","offset":0,"id":"1a-b2"}
{"datatype: null" "country_code":"eu","offset":0,"id":"2j-u3"}
{"datatype: null" "country_code":"eu","offset":0,"id":"5k-hy"}
{"datatype: null" "country_code":"eu","offset":0,"id":"1h-j3"}