问题
While reading a file I get broken UTF-8 String error whenever I have the following in my file
través
if I change it to normal e then it works.
Whats the way to fix this?
error only happens if I do line.lstrp or any other function. Just printing the lines is ok.
problem even happens when I try to match the string with regex.
回答1:
Obviously your file is not UTF-8 encoded. So, you should either take care of that (save your file using UTF-8), or tell Ruby that your strings aren't going to be UTF-8. In order to do that, you can either
- use
-E encodingcommand line switch for that - pass
-E encodingusingRUBYOPTenvironment variable - specify encoding within Ruby comment
- do
force_encodingmethod on already loaded string before operation such aslstrp
There's a lot of Ruby encoding-related reading on following blog:
http://blog.grayproductions.net/articles/understanding_m17n
来源:https://stackoverflow.com/questions/2897398/broken-utf-8-string-ruby