`
fantaxy025025
  • 浏览: 1247549 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

invalid byte sequence in GBK”问题 invalid byte sequence in UTF-8”问题

 
阅读更多

 

碰见错误:

 

test.rb:8:in `match': invalid byte sequence in GBK (ArgumentError)
        from test.rb:8:in `block in <main>'
        from test.rb:6:in `each'
        from test.rb:6:in `<main>'

 

 

原因:

平时习惯在文件头部加入# encoding: utf-8 没用的。

理论总是在问题的时候才能闪光。

看此文。Ruby1.9之字符串内编码和外编码

 

解决方法:

假如外部文件是utf-8编码。

* 读文件的时候指定外编码:utf-8。 lines = File.readlines('xxx\origin3.txt', :external_encoding => "UTF-8")

* 行内转码:line.force_encoding("UTF-8")#是修改内编码为UTF8

file=File.new(fr).tap{|f|f.set_encoding'utf-8','GBK'}

* set_encoding 'utf-8','GBK     #是将字节流转换成GBK格式的(外编码仍为UTF8)

f = File.open 'environment.rb''r:gbk' # 如果是 utf-8 编码,就用 'r:utf-8'

 f = File.open 'environment.rb''r:gbk:utf-8' # 如果是 utf-8 编码,就用 'r:utf-8'

 

为什么很多人出现此问题?

win7下:

puts "Encoding.default_external=#{Encoding.default_external}"

puts "Encoding.default_internal=#{Encoding.default_internal}" 

win7下默认输出:

Encoding.default_external=GBK

Encoding.default_internal=

看见了默认是认为外部编码gbk,但我们一般的习惯文件是utf-8的。

好了,知晓了。

 

+

+
==

==

=

 

分享到:
评论

相关推荐

    解决Invalid byte 1 of 1-byte UTF-8 sequence

    解决Invalid byte 1 of 1-byte UTF-8 sequence

    php解析xml提示Invalid byte 1 of 1-byte UTF-8 sequence错误的处理方法

    错误提示Invalid byte 1 of 1-byte UTF-8 sequence原因分析在中文版的window下java的默认的编码为GBK,也就是所虽然我们标识了要将xml保存为utf-8格式但实际上文件是以GBK格式来保存的,所以这也就是为什么能够我们...

    ruby中文文档(ruby入门级别教程)

    包括ruby用户指南,RGSS入门教程,Programming Ruby,Ruby参考手册共4部分内容。 chm格式

    invalid multibyte character sequence 870告警1

    invalid multibyte character sequence 870告警1

    SQL Server数据迁移至PostgreSQL出错的解释以及解决方案

    最近对SQL Server到PostgreSQL的数据迁移时出现了问题,返回的错误为:invalid byte sequence for encoding "UTF8": 0x00。经查证pg源代码,该问题引起的原因是sql server的字符类型字段中含有空字符0,该字符在pg中...

    pyquery报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte

    今天想使用pyquery库读取本地HTML文件时报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa1 in position 164: illegal multibyte sequence。 翻译一下就是UnicodeDecodeError: ‘gbk’编解码器无法...

    LuaUnicode icu-lua

    For example, you could use the following code snippet to count UTF-8 characters in a string you knew to be conforming (it will incorrectly count some invalid characters): local _, count = string....

    android layout XML解析错误的解决方法

    提示: org.xmlpull.v1.XmlPullParserException: PI must not start with xml (position:unknown @1:5 in java.io.InputStreamReader@47ec2770) ...问题原因: 为XML文件第一行格式错误,仔细修改第

    Chatlog_Ripper:一个帮助你从聊天记录文件中提取 URL 的小程序

    聊天记录开膛手在 WDI 中,我们共享一切。... 如果您收到错误“in `scan': invalid byte sequence in UTF-8 (ArgumentError)”,只需将您的文本日志解析为可以转换为 UTF-8 的内容(例如 )。 我将来会解决这个问题。

    Bochs - The cross platform IA-32 (x86) emulator

    Changes in 2.4.6 (February 22, 2011): Brief summary : - Support more host OS to run on: - Include win64 native binary in the release. - Fixed failures on big endian hosts. - BIOS: Support for up to...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD: Delphi/CBuilder XE8 now supported. - ADD: Delphi/CBuilder XE7 now supported. - ADD: Delphi/CBuilder XE6 now supported. - ADD: Delphi/CBuilder XE5 now supported. - ADD: Delphi/CBuilder XE4 now ...

    doctest:Haskell的python doctest的实现

    Doctest:测试交互式Haskell示例doctest是一个小程序,用于检查。 它与相似,。安装可以从获得doctest 。 通过键入以下内容进行安装: cabal install doctest确保Cabal的bindir在您的PATH 。 在Linux上: export ...

    Python编程规范PEP8.pdf

    Python编程规范PEP8( Style Guide for Python),包括命名规范,注释规则,编程推荐等等,欢迎大家下载。

    acpi控制笔记本风扇转速

    Added #pragma pack(8/4) to acobject.h to ensure that the structures in this header are always compiled as aligned. The ACPI_OPERAND_OBJECT has been manually optimized to be aligned and will not work ...

    k7 SRIO参考例程

    - CR#469678 / AR#30940 - Fixed condition which loaded in new CRC sequence on a stall just after sof received by PHY. This is a non-concern for Xilinx buffer users. - Virtex-4 4x core may ...

    CE中文版-启点CE过NP中文.exe

    Fixed freeze with allow increase/decrease for 8 byte long values Fixed several issues where minimizing a window and then close it would hang CE Fixed file scanning Fixed crashes when editing memory in...

    微软内部资料-SQL性能优化3

    Byte 8-9: IndexID – 0x0001 Byte 10-16: Hash Key value – 0x 02014F0BEC4E For more information about how to decode this value, see also… Inside SQL Server 2000, pages 803 and 806. Key Range ...

    iuhyiuhkjh908u0980

    在windows系统中,命令行中执行ant命令时,当指定的 构建脚本文件中包含中文字符,而构建脚本文件的编码是UTF-8时将会 Invalid byte 1 of 1-byte UTF-8 sequence. 的错误.这个问题尚未 知解决,故先采用GBK的编码. 2.在...

    S7A驱动720版本

    - The passing of invalid OPC Item IDs caused a memory leak in the driver's global memory. After the global memory was exhausted, the driver crashed. - The writing of strings failed when using ...

Global site tag (gtag.js) - Google Analytics