从POST请求运行页面时,编码不正确

时间:2019-04-17 20:05:41

标签: php python utf-8 character-encoding

我正在从python脚本上传一个csv文件到我的网页。

Python代码:

url = 'http://MYURL'
files = {'file': ('report.csv', report)} // report is my csv file
resp = requests.post(url, files=files)

PHP代码:

header("Content-Type: text/html; charset=utf-8"); // Having this made no difference to output
$csv = array_map('str_getcsv', file($_FILES['file']['tmp_name']));
$keys = array_shift($csv);
foreach ($csv as $index => $values) { // For every row in csv
    $result = array_combine($keys, $values);
    $result = array_map('utf8_encode', $result); // Add UTF-8 encoding
    var_dump(mb_detect_encoding($result['Name']));
    var_dump($result['Name']);
}

我是直接通过自己上传文件还是使用Python脚本上传文件直接运行PHP脚本,mb_detect_encoding()返回'UTF-8'。

但是,直接运行PHP脚本会$result['Name']返回ü(正确)。从Python脚本运行会返回ü(不正确)。

要上传的.csv文件与ISO-8859-1完全相同,并编码为ISO-8859-1。为什么从Python脚本运行会导致编码不正确?

编辑:

这是hexdump -C csvfile.csv

的结果
00000000  22 58 58 58 58 20 fc 62  65 72 22    |"XXXX .ber"|
0000000b

0 个答案:

没有答案