Ruby `split': invalid byte sequence in UTF-8 (ArgumentError)

后端 未结 2 1552
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-12 10:04

I am trying to populate the movie object, but when parsing through the u.item file I get this error:

`split\': invalid byte sequence in UTF-8

2条回答
  •  不思量自难忘°
    2021-02-12 10:08

    I had to force the encoding of each line to iso-8859-1 (which is the European character set)... http://en.wikipedia.org/wiki/ISO/IEC_8859-1

    a=[]
    IO.foreach("u.item") {|x| a << x}
    m=[]
    a.each_with_index {|line,i| x=line.force_encoding("iso-8859-1").split("|"); m[i]=x}
    

提交回复
热议问题