Python request.header unicode字符错误

2024-10-01 09:28:37 发布

您现在位置:Python中文网/ 问答频道 /正文

以下代码给出了一个错误:

headers = {'Path': '00ab536d1c45d0e21f2726c70ca78a21_كلمة سمو.docx',
           'IssueNo': '', 'IssueDate': '2020-01-27', 'IssueYear': '',
           'CountryName': '', 'DocSource': '66',
           'FileName': '00ab536d1c45d0e21f2726c70ca78a21_كلمة  الأردن.docx',
           'DocNumber': '', 'CreateDMSDoc': '1',
           'apiKey': '4B30A7BB-05E8-4F7D-A195-093CFA578995'}

response = requests.post('http://localhost/FileUploadService/Api/UploadtStream', files=file, headers=headers)

错误是:

UnicodeEncodeError: 'latin-1' codec can't encode characters in position 33-36: ordinal not in range(256)

我该如何解决这个问题


Tags: path代码in错误filenameheadersdocxapikey
2条回答

这是无法用拉丁语-1编码的阿拉伯字符的问题,您可以尝试使用utf-16或utf-8编码,并在python脚本开头添加以下注释: #编码:utf-8--

您需要将字符集设置为utf-8或适当的阿拉伯语字符集。拉丁语无法对阿拉伯语字符进行编码:

headers={'Path': '00ab536d1c45d0e21f2726c70ca78a21_كلمة سمو الوزير منتدى المستقبل الأردن.docx'.encode('utf-8'),
'IssueNo': '', 'IssueDate': '2020-01-27', 'IssueYear': '', 'CountryName': '',
'DocSource': '66',
'FileName': '00ab536d1c45d0e21f2726c70ca78a21_كلمة سمو الوزير منتدى المستقبل الأردن.docx'.encode('utf-8'),
'DocNumber': '', 'CreateDMSDoc': '1',
'apiKey': '4B30A7BB-05E8-4F7D-A195-093CFA578995'}

您需要将每个阿拉伯字符串具体编码为utf-8

相关问题 更多 >