并排打印值

时间:2019-05-25 21:59:52

标签: python json python-3.x

我正在尝试并排输出值。我已经粘贴了我要完成的示例和代码。这可能吗?

import requests
import json

r = requests.get('https://api.website.com',headers = {"content-type": "application/json", "X-Auth-Key":"key-here","X-Auth-Email":"email@"})

#packages_json = r.json()
#packages_list = json.dumps(packages_json, indent=2)



with open("C:\\Users\\vm\\Desktop\\newjson.json") as access_json:
#   print(packages_list, file = access_json)
#   data = pd.read_json(access_json)
#   print(data)
    read_content = json.load(access_json)
#print(read_content)


#for i in range(0,len(read_content['result']['totals'])):

unique = read_content['result']['totals']['uniques']['all']
requests = read_content['result']['totals']['requests']['all']
print("Unique Visitors " + str(unique))
print("Total Requests " + str(requests))

我希望它看起来像什么

Unique Visitors 862 next-value1 next-value2 next-value3

Total Requests 2866 next-value1 next-value2 next-value3

基本上,它是从json文件中读取值,该文件每小时要从API中获取数据。我要完成的工作是,该脚本每小时运行一次,并查看文件,并获取当前值放置在现有值旁边的所有内容。

1 个答案:

答案 0 :(得分:0)

如果unique变量是列表,则可以执行以下操作:

print("Unique Visitors " + '   '.join(unique))

但是最好使用日志记录模块,这样它将以您想要的格式进入特定的日志文件。