将字符串连接到返回的json列表python的末尾

问题描述

嗨,我正在使用下面的代码从URL返回一些json,它以我想要的方式将结果作为列表返回,但是我想为每个返回的结果添加一个字符串。

在for循环中打印n可以工作,但是我不知道在哪里添加添加到结果中的字符串,我尝试过的所有操作都会引发有关将字符串与列表混合的错误。我已经阅读了有关数组的内容,可以将数据添加到数组中并仅显示某些结果,但不确定从何处开始将数据添加到结果中。

例如。

我明白了

Result1
Result2
Result3,etc,

但是我想回来

String Result1
String Result2
String Result3.

字符串将是常量,但结果将是返回的值。

import urllib.request as request
import json


with request.urlopen('https://jsondatalist') as response:
    if response.getcode() == 200:
        source = response.read()
        data = json.loads(source)

        for n in (data):
            print (n)

    else:
        print('An error occured while attempting to retrieve data from source')

解决方法

似乎您在问字符串内插。

在python> 3.6上,您可以进行print(f"String {n}")

在旧版本的python上,您可以进行print("String {}".format(n))