Python json.dump文件包含%



我有一个简单的python脚本,可以将字典写入文件:

from datetime import datetime, timedelta
import json
import time

token_file_name = "dict.json"
my_dict = {"access_token": "MbwrA4HY1rjUJLrho",
"expires_in": 1800,
"refresh_token_expires_in": 7776000
}   
with open( token_file_name, 'w', encoding='utf-8') as f:
json.dump(my_dict, f, ensure_ascii=False, indent=4)
f.close()

我在JSON对象的末尾看到一个奇怪的字符(%(:

╰─➤  cat dict.json 
{
"access_token": "MbwrA4HY1rjUJLrho",
"expires_in": 1800,
"refresh_token_expires_in": 7776000
}%

两个问题:

  1. 如何消除此问题?

  2. 当读取包含多个JSON对象的文件时,如果"%"显示在两个JSON对象之间,这种情况会发生吗?

百分号不在您的文件中。当输出末尾没有换行符时,Zsh会这样做。

试试这个:

$ echo -n test
test%

-n选项省略了echo通常插入的换行符。

最新更新