用Ruby(IRB)打印CP850编码的字符串 [英] Printing a CP850 encoded string with Ruby (IRB)

查看:166
本文介绍了用Ruby(IRB)打印CP850编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开包含阿拉伯文字(其编码为CP850)的文本文件(test.txt),然后将其内容打印到STDOUT中:

I want to open a text file (test.txt) that contains arabic text (its encoding is CP850), then print its content to STDOUT :

# coding : CP850
STDOUT.set_encoding(Encoding::CP850); # not sure if it's necessary
open('G:/test.txt',?r){|f|
    f.read.each_char{|c| print c};
    # or puts f.read;
}
gets

但它不打印阿拉伯字符,输出是一些符号和随机字符。
使用Ruby 2.2.3

but it does not print the arabic characters, the output is some symbols and random characters. Using Ruby 2.2.3

推荐答案

将文件的编码更改为utf-8。

Change the encoding of the file to utf-8.

我不知道Ruby是如何实现的,但Django(使用Python 3的较新版本),它是:

I don't know how this is accomplished in Ruby, but Django (the newer ones using Python 3), it's:

open('filename.txt', w, 'utf-8)

如果您使用的是Python 2,那将会更加困难。如果是这样,值得升级到3,因为它是原生的unicode,并做任何事情与阿拉伯语很容易。

If you're using Python 2 it will be slightly more difficult. If so, it's worth upgrading to 3 just because it's native unicode and makes doing anything with Arabic a lot easier.

这篇关于用Ruby(IRB)打印CP850编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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