Python 如何从json解码值?
我什么都试过了,但都没用。 我需要将数据从cp1361解码到utf-8。以下是使用的代码:Python 如何从json解码值?,python,python-3.x,Python,Python 3.x,我什么都试过了,但都没用。 我需要将数据从cp1361解码到utf-8。以下是使用的代码: import json import request path = 'result.json' with open(path, 'r') as f: data = json.loads(f.read()) for i in data['data']: a= i['CitySender'].encode() print(a.decode('utf-8')) results.js
import json
import request
path = 'result.json'
with open(path, 'r') as f:
data = json.loads(f.read())
for i in data['data']:
a= i['CitySender'].encode()
print(a.decode('utf-8'))
results.json
{
"success": true,
"data": [
{
"Number": "",
"Redelivery": 0,
"RedeliverySum": 0,
"RedeliveryNum": "",
"RedeliveryPayer": "",
"OwnerDocumentType": "",
"LastCreatedOnTheBasisDocumentType": "",
"LastCreatedOnTheBasisPayerType": "",
"LastCreatedOnTheBasisDateTime": "",
"LastTransactionStatusGM": "",
"LastTransactionDateTimeGM": "",
"DateCreated": "28-04-2020 18:22:12",
"CheckWeight": 0,
"SumBeforeCheckWeight": 0,
"PayerType": "Sender",
"RecipientFullName": "",
"RecipientDateTime": "04.05.2020 18:53:33",
"ScheduledDeliveryDate": "02-05-2020 12:00:00",
"PaymentMethod": "Cash",
"CargoDescriptionString": "",
"CargoType": "Parcel",
"CitySender": "\\u041a\\u0438\\u0457\\u0432",
"CityRecipient": "\\u041a\\u0438\\u0457\\u0432",
"WarehouseRecipient": "",
"CounterpartyType": "PrivatePerson",
"AfterpaymentOnGoodsCost": 0,
"ServiceType": "WarehouseWarehouse",
"UndeliveryReasonsSubtypeDescription": "",
"WarehouseRecipientNumber": 1,
"LastCreatedOnTheBasisNumber": "",
"PhoneRecipient": "",
"RecipientFullNameEW": "",
"MarketplacePartnerToken": "",
"ClientBarcode": "",
"RecipientAddress": "",
"CounterpartyRecipientDescription": "\\u041f\\u0440\\u0438\\u0432\\u0430\\u0442\\u043d\\u0430 \\u043e\\u0441\\u043e\\u0431\\u0430",
"CounterpartySenderType": "PrivatePerson",
"DateScan": "0001-01-01 00:00:00",
"PaymentStatus": "",
"PaymentStatusDate": "",
"AmountToPay": "",
"AmountPaid": "",
"LastAmountTransferGM": "",
"LastAmountReceivedCommissionGM": "",
"DocumentCost": 60,
"DocumentWeight": 2,
"AnnouncedPrice": 200,
"UndeliveryReasonsDate": "",
"RedeliveryPaymentCardDescription": "",
"OwnerDocumentNumber": "",
"InternationalDeliveryType": "",
"WarehouseSender": "",
"DeliveryTimeframe": "",
"VolumeWeight": "2.00",
"SeatsAmount": "1",
"ActualDeliveryDate": "2020-04-30 14:36:41",
"CardMaskedNumber": "",
"BarcodeRedBox": "",
"Packaging": [
{
"Description": "\\u041a\\u043e\\u0440\\u043e\\u0431\\u043a\\u0430 (2 \\u043a\\u0433)"
}
],
"AviaDelivery": 0,
"OnlineCreditStatus": "",
"AdjustedDate": "",
"FreeShipping": 0,
"CheckWeightMethod": "",
"Status": "\\u0412\\u0456\\u0434\\u043f\\u0440\\u0430\\u0432\\u043b\\u0435\\u043d\\u043d\\u044f \\u043e\\u0442\\u0440\\u0438\\u043c\\u0430\\u043d\\u043e",
"StatusCode": "9",
"DatePayedKeeping": "",
"OnlineCreditStatusCode": ""
}
],
"errors": [],
"warnings": [],
"info": [],
"messageCodes": [],
"errorCodes": [],
"warningCodes": [],
"infoCodes": []
}
我正在解码的字符串:
\\u041a\\u0438\\u0457\\u0432
结果:
\u041a\u0438\u0457\u0432
Київ
我在CP1361中得到字符串:
在另一个草图中,我尝试了以下代码:
from sys import getdefaultencoding
getdefaultencoding() # utf-8
data = '\u041a\u0438\u0457\u0432'.encode()
print(data.decode('utf-8'))
结果:
\u041a\u0438\u0457\u0432
Київ
我得到了正确的结果。我想我需要双重解码,但我不确定。您可以尝试以下方法:
x = "\\u0412\\u0456\\u0434\\u043f\\u0440\\u0430\\u0432\\u043b\\u0435\\u043d\\u043d\\u044f \\u043e\\u0442\\u0440\\u0438\\u043c\\u0430\\u043d\\u043e"
x.encode('utf_8').decode('unicode_escape')
#'Відправлення отримано'
因此,我已经用您的json文件对此进行了测试,应该可以:
def decodejohab(s):
return str(s.encode("utf-8"), "unicode_escape")
import json
import request
path = 'result.json'
with open(path, 'r') as f:
jsonfile = json.loads(f.read())
for i, v in enumerate(jsonfile["data"]):
print(decodejohab(jsonfile["data"][i]["CitySender"]))
.编码'cp1361'。解码'cp1361'。编码'utf-8'。解码'utf-8'没有任何意义。顺便说一句,你对我们性取向的假设可能并不适用于所有人。对不起,有一些错误。我更正了文本,它起作用了。谢谢这是我的错误,库是请求的,所以我不确定:D