我當前的 Python Appium 測驗自動化框架使用json.load()從 JSON 檔案中讀取資料
當呼叫 json.load()方法時,存盤在 JSON 中的值“Ελλ?δα”被轉換為“?????????′?±” 。
請指出一個解決方案,如果我可以保持實際的字串值“Ελλ?δα”
testdata.json 中的 JSON 資料
{
"country" : {"country_name" : "Ελλ?δα"}
}
.py 檔案中的 JSON 加載
with open(file_location) as test_data_obj:
pytest.data = json.load(test_data_obj)
這將列印出“?????????′?±”
print(pytest.data["country"]["country_name"])
uj5u.com熱心網友回復:
使用with open(file_location,encoding='utf-8'). 無論出于何種原因,您的系統都使用拉丁語 1 作為默認編碼而不是 UTF8。
檔案沒問題。這個頁面和 99.9% 的網頁一樣是 UTF8。這就是為什么你可以毫無問題地寫出Ελλ?δα。
Python 3 字串也是 Unicode。如果你"Ελλ?δα"在 Python 控制臺中輸入,你會得到這個字串。
要獲得您發布的內容,我必須使用以下方法解碼位元組latin-1:
>>> "Ελλ?δα".encode('utf-8').decode('latin-1')
'?\x95???????′?±'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/519936.html
下一篇:地震資料分組問題
