ReadLine读到的中文乱码,Import却正常
mathematica吧
全部回复
仅看楼主
level 6
泥萌都👀 楼主
SetDirectory[NotebookDirectory[]];
Import["record.txt"]
ReadFile = OpenRead["record.txt"];
ReadLine[ReadFile]
ReadLine[ReadFile]
Close[ReadFile]
-----------------------
代码如上。请问ReadLine有办法读中文吗?怎么做?
环境:Win11家庭中文版+Mathematica 12.3.1.0中文版
2022年07月29日 07点07分 1
吧务
level 10
默认是Character8,中文肯定没戏。考虑用BinaryRead系列函数,自己来FromCharacter,尤其是UTF8若要实时处理会很麻烦。
2022年07月29日 10点07分 2
*FromCharacterCode
2022年07月29日 10点07分
吧务
level 13
这种情况转码可行
ReadLine[f] // StringToByteArray[#, "ISO8859-1"] & // ByteArrayToString[#, "UTF-8"] &
建议使用能直接设置编码参数的函数。
mma自带一重unicode转码,有些函数实现的有缺陷,用着会很难受
2022年07月29日 14点07分 4
level 6
泥萌都👀 楼主
好吧。以前都ReadLine英文的,这次第一次处理中文……
2022年07月29日 14点07分 5
吧务
level 15
参看SE帖子《Can I read Chinese characters with ReadList correctly?》(mathematica.stackexchange.com/q/48570/1871)
2022年08月06日 07点08分 6
1