如何从文件#路径获取utf8字符 [英] How can I get utf8 characters from File#path

查看:242
本文介绍了如何从文件#路径获取utf8字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件#路径给我拉丁字符 - 有没有办法让它给我utf8字符,或者我应该只转换什么返回?如果是,最好/最简单的转换方式是什么?



详细



所以,我知道我可以这样做:

  Iconv.new('UTF-8','LATIN1')。iconv File.basename(file.path))

但我想知道是否有更优雅的方式

这是特别重要的,因为某种原因我回来了不同系统上的不同字符集。在我的OS X dev机器,它看起来像我回来utf8。在我的linux服务器上,拉丁语-1。

解决方案

请参阅$ LANG和$ LC_CTYPE(环境变量)。



这些变量还决定了1.9中编码默认值的默认值,因此,如果您以后将代码移植到1.9,那么今天的更改也会起作用。



注意在这方面,Windows是一个略有不同的野兽,因此您可能需要更多的信息来解决这个问题。


File#path is giving me Latin-1 characters -- is there a way to get it to give me utf8 characters, or should I just convert what it returns? If so, what's the best/easiest way to convert?

elaboration

So, I know I can do this:

Iconv.new('UTF-8','LATIN1').iconv(File.basename(file.path))

But I'm wondering if there is a more elegant way to tell File to give me utf8 to begin with.

This is especially important because for some reason I get back a different charset on different systems. On my OS X dev machine, it looks like I get back utf8. On my linux server, latin-1.

解决方案

See $LANG and $LC_CTYPE (environment variables).

These variables also determine the default value for the encoding defaults in 1.9, and so changes you make today will also work if you later port your code to 1.9.

N.B. Windows is a slightly different beast in this regard, so you may need further information to tackle that.

这篇关于如何从文件#路径获取utf8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆