【问题标题】:python error while looping from a list json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)从列表json.decoder.JSONDecodeError循环时出现python错误:期望值:第1行第1列(char 0)
【发布时间】:2021-05-21 19:18:20
【问题描述】:

我正在制作一个脚本,用来自 api 的响应填充文本文档。该 api 被要求将用户名从列表转换为通用唯一标识符。我不断收到此错误,无法找到解决方法。 “json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)”

accounts.txt 示例

knapplace
Coppinator
tynow
Pman59
ButterMusty
FlyHighGuy13
Seyashi
fluzzygirl1
SquidMan55
leonrules9
BarthGimble
MTR_30
Darkshadow402
Deathmyster
Team_Everlook
Sheathok
KCFrost
mendog
Allfaal117
theLP25D
Zimyx
Blurrnis
redboy678
moose_breeder
kaser12345
import requests
import json

file1 = open('accounts.txt', 'r')

usernames = []

for line in file1:

    stripped_line = line.strip()
    usernames.append(stripped_line)

file1.close()

for x in usernames:

    username = str(x)

    url = ("https://api.mojang.com/users/profiles/minecraft/"+username+"?at=1462770000")

    y = requests.get(url)
    y_data = y.json()
    uuid = y_data['id']

    uuids = []
    uuids.append(uuid)

    file2 = open('uuids.txt', 'w')
    file2.writelines(uuids)
    file2.close()

    file2 = open('uuids.txt', 'r')
    lines = file2.readlines()

【问题讨论】:

  • 您能否提供一个示例用户名列表,以便我们尝试调试您的代码?由于我们没有accounts.txt 文件,因此仅通过查看您的代码很难看出发生了什么。
  • 我已经为你添加了一个示例

标签: python json python-3.x list python-requests


【解决方案1】:

注意:@Ali 非常注重检查是否有空回复。有了这个修复,它对我来说就像一个冠军,还有一些其他的小改动:

  • 使用 OP 提供的用户名,而不是从文件中读取。
  • uuids 的初始化移出 for 循环,以避免为每个用户名重置它。
  • 将文件 i/o 内容修改为我更习惯使用的内容。 ;^)
import requests
import json

usernames = [
    "knapplace",
    "Coppinator",
    "tynow",
]

uuids = []
for x in usernames:

    username = str(x)

    url = ("https://api.mojang.com/users/profiles/minecraft/"+username+"?at=1462770000")

    y = requests.get(url)
    if len(y.content) == 0:
        continue  # Skip processing this username

    y_data = y.json()
    uuid = y_data['id']

    uuids.append(uuid)

with open('uuids.txt', 'w') as f:
    for uuid in uuids:
        f.write(uuid + '\n')

with open('uuids.txt', 'r') as f:
    read_data = f.read()

print(read_data)

输出:

c9998bafea3146d5935f4e215b6b4351
5c321f81409847a0907c4b30c342217f
9f206def69bf407fbab6de7c9b70ff80

【讨论】:

    【解决方案2】:

    我检查了您粘贴的 URL。如果用户不存在,API 不会返回任何内容,但仍会返回成功状态。这就是错误的意思——它期望有一个从char 0 开始的 JSON 对象。

    本质上,您需要先处理响应为空的情况,然后再尝试通过检查y.content 来执行y.json()。如果y.content 为空,则跳过处理当前用户名并转到下一个。

    y = requests.get(url)
    if len(y.content) == 0:
       continue  # Skip processing this username
    
    # The rest of the code only runs if y.content is not empty.
    y_data = y.json()
    uuid = y_data['id']
    

    【讨论】:

    • 如何检查 y.content?一个简单的 if y.content != None?
    • y.content 是一个字节串。您可以检查y.content == b''len(y.content) == 0
    • 它返回了一个“uuid”并返回了同样的错误?
    • 我在答案中添加了示例代码 sn-p。您是否在执行与我发布的内容类似的操作但仍收到错误消息?
    • 最后一期,脚本正在按预期工作,除了它正在替换以前的 uuid
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2020-11-11
    • 2019-07-04
    • 2017-05-09
    • 2019-08-13
    相关资源
    最近更新 更多