在使用zip时,我知道我的文本中有一个{}不会让zip函数多次运行。我知道它是{}因为当我擦除它们时。代码运行完美。但我需要{}在那个文本中。 为了给你全局, 我有一个现成的文本,我想从3个列表中“解包”值(这三个列表在此代码行之前定义),每个列表都会到达适当的位置。
text = '''curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"name": "{}",
"lei": " ",
"parentId": "0",
"status": "ACTIVE",
"type": "1",
"country": "{}",
"region": "1",
"address": "{}",
"domains": "string.com"
}' 'http://qa-eu-entities.capitol.is/org'
'''
for i in zip(df['Name'], df['Country'], df['Address']):
print(text.format(*i))
我得到的错误是KeyError
KeyError: '\n "name"'
我尝试使用json.loads并在文本上调用它,但没有成功。
知道可以采取哪些不同的做法?
答案 0 :(得分:0)
文字问题包含额外的{
,这就是导致错误的原因。我建议像这样使用,
In [17]: text = """curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
...: "name": "%s",
...: "lei": " ",
...: "parentId": "0",
...: "status": "ACTIVE",
...: "type": "1",
...: "country": "%s",
...: "region": "1",
...: "address": "%s",
...: "domains": "string.com"
...: }' 'http://qa-eu-entities.capitol.is/org'
...: """
In [19]: print(text %('hai','hey','test'))
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"name": "hai",
"lei": " ",
"parentId": "0",
"status": "ACTIVE",
"type": "1",
"country": "hey",
"region": "1",
"address": "test",
"domains": "string.com"
}' 'http://qa-eu-entities.capitol.is/org'
你的情况就是这样,
for i in zip(df['Name'], df['Country'], df['Address']):
print(text %(i))