带有非英文字符的 Haskell IO
看看这个,我正在尝试
appendFile "out" $ show 'д'
“д”是俄语字母表中的字符。 之后“out”文件包含:
'\1076'
我的理解是字符“д”的unicode数字代码。为什么会出现这样的情况呢?我怎样才能得到我的角色的正常表现?
有关更多信息,效果很好:
appendFile "out" "д"
谢谢。
Look at this , i am try
appendFile "out" $ show 'д'
'д' is character from Russian alphabet.
After that "out" file contains:
'\1076'
How i understand is the unicode numeric code of character 'д'. Why is it happens ? And How i can to get the normal representation of my character ?
For additional information it is works good:
appendFile "out" "д"
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
show
对 ASCII 范围之外的所有字符(以及 ASCII 范围内的一些字符)进行转义,因此不要使用show
。因为“д”工作正常,所以就使用它。如果你不能,因为 д 实际上在变量内部,你可以使用
[c]
(其中 c 是包含该字符的变量。如果你需要用单引号将它引起来(就像 show 那样) ),您可以使用['\'', c, '\'']
。show
escapes all characters outside the ASCII range (and some inside the ASCII range), so don't useshow
.Since "д" works fine, just use that. If you can't because the д is actually inside a variable, you can use
[c]
(where c is the variable containing the character. If you need to surround it by single quotes (like show does), you can use['\'', c, '\'']
.阅读您对我的评论的回复后,我认为您的情况是您有一些数据结构,可能是
[(String,String)]
类型,并且您希望输出它以用于调试目的。使用show
会很方便,但它会转义非 ASCII 字符。这里的问题不在于 unicode,您需要一个能够正确格式化数据以供显示的函数。我认为
show
不是正确的选择,部分原因是转义某些字符存在问题。您需要的是像Show
这样的类型类,但它显示数据以供读取而不是转义字符。也就是说,您需要一个漂亮的打印机,它是一个提供格式化数据以供显示的函数的库。 Hackage 上有几种漂亮的打印机,我会看看 uulib 或 wl-pprint 启动。我认为两者都适合,无需太多工作。这是 uulib 工具的示例。使用
Pretty
类型类代替 Show,该库附带了许多有用的实例。在 ghci 中运行:
如果您想输出到文件而不是控制台,可以使用 hPutDoc 函数输出到句柄。您还可以调用 renderSimple 来生成 SimpleDoc,然后在构造函数上进行模式匹配来处理输出,但这可能会更麻烦。无论你做什么,都要避免
show
:你也可以编写自己的类似于 show 的类型类,但按照你喜欢的格式进行格式化。如果您选择这条路线,
Text.Printf
模块会很有帮助。After reading your reply to my comment, I think your situation is that you have some data structure, maybe with type
[(String,String)]
, and you'd like to output it for debugging purposes. Usingshow
would be convienent, but it escapes non-ASCII characters.The problem here isn't with the unicode, you need a function that will properly format your data for display. I don't think
show
is the right choice, in part because of the problems with escaping some characters. What you need is a type class likeShow
, but one that displays data for reading instead of escaping characters. That is, you need a pretty-printer, which is a library that provides functions to format data for display. There are several pretty-printers available on Hackage, I'd look at uulib or wl-pprint to start. I think either would be suitable without too much work.Here's an example with the uulib tools. The
Pretty
type class is used instead of Show, the library comes with many useful instances.running this in ghci:
If you want to output to a file instead of the console, you can use the
hPutDoc
function to output to a handle. You could also callrenderSimple
to produce aSimpleDoc
, then pattern match on the constructors to process output, but that's probably more trouble. Whatever you do, avoidshow
:You could also write your own type class similar to show but formatted as you like it. The
Text.Printf
module can be helpful if you go this route.使用数据.文本。它为 IO 提供区域设置感知和编码支持。
Use Data.Text. It provides IO with locale-awareness and encoding support.
在网络上快速搜索“UTF Haskell”应该会为您提供良好的链接。最推荐的包可能是 text 包。
A quick web search for "UTF Haskell" should give you good links. Probably the most recommended package is the text package.
要按节目显示国家字符,请输入您的代码:
您可以尝试:
To display national characters by show, put in your code:
You can try then:
我之前的解决方案中没有引号。另外,我现在把代码放在模块中,模块必须导入到你的程序中。
给初学者的信息:请记住,该节目不显示任何内容。 show 将数据转换为带有附加格式字符的字符串。
我们可以在WinGHCi中尝试:
通过 WinGHCi 自动
或手动
在要显示的代码中:
我正在为 Google 添加标签“polskie znaki haskell”。
There were no quotes in my previous solution. In addition, I put the code in the module now and the module must be imported into your program.
Information for beginners: remember that the show does not display anything. show converts data to string with additional formatting characters.
We can try in WinGHCi:
automaticaly by WinGHCi
or manualy
In code to display:
I'm adding tag "polskie znaki haskell" for Google.