python - 如何将字符串列表转换为单个分隔字符串?
问题描述
我一直在寻找几十个线程,但找不到答案,但希望它不会太难。我需要获取一个字符串列表并将它们转换为以逗号分隔的单个字符串。还需要该单个字符串由变量保存。
我正在使用此脚本从 API 调用关键字标签。
import requests, csv
api_key = 'abc'
api_secret = 'xyz'
image_url = 'some url'
response = requests.get(
'the api url',
auth=(api_key, api_secret))
data = response.json()
for tags in data['result']['tags']:
tags = tags['tag']['en']
img_list = tags.split(",") #convert values to a list
list_str = ','.join([str(elem) for elem in img_list]) #converts list to a string
print(list_str)
我得到的数据是 JSON,然后我通过一个循环来从 JSON 的其余部分获取我们的标签。我将其转换为列表,然后转换为字符串,但它不是单个字符串。
JSON 示例:
{'result': {'tags': [{'confidence': 35.0027923583984, 'tag': {'en': 'machinist'}}, {'confidence': 30.3697471618652, 'tag': {'en': 'seller'}}, {'confidence': 28.2139053344727, 'tag': {'en': 'man'}}, {'confidence': 27.542501449585, 'tag': {'en': 'work'}}, {'confidence': 23.9936943054199, 'tag': {'en': 'worker'}}, {'confidence': 23.8494567871094, 'tag': {'en': 'working'}}, {'confidence': 23.012264251709, 'tag': {'en': 'person'}}, {'confidence': 21.2697811126709, 'tag': {'en': 'male'}}, {'confidence': 21.2244606018066, 'tag': {'en': 'job'}}, {'confidence': 18.9516372680664, 'tag': {'en': 'people'}}, {'confidence': 18.8158016204834, 'tag': {'en': 'construction'}}, {'confidence': 17.44016456604, 'tag': {'en': 'equipment'}}, {'confidence': 17.0697708129883, 'tag': {'en': 'industry'}}, {'confidence': 16.7942485809326, 'tag': {'en': 'stall'}}, {'confidence': 16.7873115539551, 'tag': {'en': 'steel'}}, {'confidence': 14.6504859924316, 'tag': {'en': 'tool'}}, {'confidence': 13.7434034347534, 'tag': {'en': 'occupation'}}, {'confidence': 13.6098012924194, 'tag': {'en': 'industrial'}}, {'confidence': 12.9942970275879, 'tag': {'en': 'machine'}}, {'confidence': 12.951189994812, 'tag': {'en': 'barbecue'}}, {'confidence': 12.873197555542, 'tag': {'en': 'men'}}, {'confidence': 12.8699989318848, 'tag': {'en': 'iron'}}, {'confidence': 12.6433362960815, 'tag': {'en': 'labor'}}, {'confidence': 12.5361061096191, 'tag': {'en': 'old'}}, {'confidence': 12.5158472061157, 'tag': {'en': 'skill'}}, {'confidence': 12.329460144043, 'tag': {'en': 'carpenter'}}, {'confidence': 11.9580173492432, 'tag': {'en': 'home'}}, {'confidence': 11.7145328521729, 'tag': {'en': 'manual'}}, {'confidence': 11.4941291809082, 'tag': {'en': 'repair'}}, {'confidence': 10.9860734939575, 'tag': {'en': 'adult'}}, {'confidence': 10.698281288147, 'tag': {'en': 'factory'}}, {'confidence': 10.5328407287598, 'tag': {'en': 'building'}}, {'confidence': 10.4540967941284, 'tag': {'en': 'metal'}}, {'confidence': 10.4486179351807, 'tag': {'en': 'tools'}}.........
print(img_list)
给我:
['machinist']
['seller']
['man']
['work']
['worker']
['working']
['person']
etc...
print(list_str)
给了我最接近的结果,但它们在不同的行上并且不是一个字符串:
machinist
seller
man
work
worker
working
person
etc....
我也尝试过csv = ",".join(tags)
,但它用逗号分隔值本身,如下所示
m,a,c,h,i,n,i,s,t
s,e,l,l,e,r
m,a,n
w,o,r,k
w,o,r,k,e,r
w,o,r,k,i,n,g
p,e,r,s,o,n
问题:
如何将数据作为逗号分隔的字符串取回,例如:
machinist,seller,man,work,worker,working,person
?
解决方案
There is a problem with getting items from json. Here is the way to get all items in image_list
correctly:
data = {'result':
{'tags': [
{'confidence': 35.0027923583984,
'tag': {'en': 'machinist'}},
{'confidence': 30.3697471618652,
'tag': {'en': 'seller'}},
{'confidence': 28.2139053344727,
'tag': {'en': 'man'}},
{'confidence': 27.542501449585,
'tag': {'en': 'work'}},
{'confidence': 23.9936943054199,
'tag': {'en': 'worker'}},
{'confidence': 23.8494567871094,
'tag': {'en': 'working'}},
{'confidence': 23.012264251709,
'tag': {'en': 'person'}}
]
}
}
image_list = []
for tags in data['result']['tags']:
# Check whether confidence is > 20 or not
# if so add it to list, else do nothing
if tags['confidence'] > 20:
image_list.append(tags['tag']['en'])
# image_list -> ['machinist', 'seller', 'man', ...]
result = ",".join(image_list)
# -> 'machinist,seller,man,.."