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

查看:34
本文介绍了使用 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天全站免登陆