简体   繁体   English

Haskell-将Unicode(字符串,字符)保存到文件

[英]Haskell - save unicode (String, characters) to file

environment: windows 7 64, ghci 7.8.3 环境:Windows 7 64,ghci 7.8.3

IN GHCI 在GHCI

\197 <-> Ć

Prelude> putStrLn "\197"
*** Exception: <stdout>: hPutChar: invalid argument (invalid character)

Finally 最后

I try use writeFile function to save String to file; 我尝试使用writeFile函数将String保存到文件;

writeFile "filename.txt" "ćĆ"
-- content in "filename.txt" -> †Ź

how to write to character quotation mark (") 如何写字符引号(“)

content2 = "\"http:\\www.google.com\""
writeFile "filename2.txt content2
  • how to store in file content2 without Escaping character 如何在不转义字符的情况下存储在文件content2
  • because I want to store something like href="http:\\\\www.google.com" 因为我要存储href="http:\\\\www.google.com"

If this is wrong way, how to download webpage (html) and save it on hdd and then open it in web browser, 如果这是错误的方法,如何下载网页(html)并将其保存在硬盘上,然后在网络浏览器中将其打开,

By default, a newly created handle is affected the locale encoding the OS reported (see System.IO.localeEncoding ). 默认情况下,新创建的句柄会影响报告的操作系统的语言环境编码(请参见System.IO.localeEncoding )。 In Windows that may be a very old codepage... So one way is to change the codepage to 65001 (with chcp 65001 ) prior to launching the Haskell executable. 在Windows中,这可能是一个非常旧的代码页...因此,一种方法是在启动Haskell可执行文件之前将代码页更改为65001(使用chcp 65001 )。 If that is unsatisfactory, you can change the encoding of any Handle in your code with System.IO.hSetEncoding (even stdout) or even use ByteString IO (binary) and do the encoding/decoding yourself (probably overkill if you don't need very precise control over it). 如果这不能令人满意,则可以使用System.IO.hSetEncoding (甚至stdout)更改代码中任何Handle的编码,甚至可以使用ByteString IO(二进制),然后自己进行编码/解码(如果不需要,可能会过大对其进行非常精确的控制)。 Note that this means that writeFile can't be used since you don't see the handle, you'll have to write your own variant : 请注意,这意味着无法使用writeFile,因为您看不到该句柄,因此必须编写自己的变体:

writeFileWithEncoding fp content enc =
  withFile fp WriteMode $ \h -> do
    hSetEncoding h enc
    hPutStr h content

Or just a version specialized for utf8. 或者只是专用于utf8的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM