title: File.readlines invalid byte sequence in UTF-8 (ArgumentError)
body: <p>I am processing a file which contains data from the web and encounter <b>invalid byte sequence in UTF-8 (ArgumentError)</b> error on certain log files.</p>
  
  <pre><code>a = File.readlines('log.csv').grep(/watch\?v=/).map do |s|
  s = s.parse_csv;
  { timestamp: s[0], url: s[1], ip: s[3] }
  end
  puts a
  </code></pre>
  
  <p>I am trying to get this <a href="https://stackoverflow.com/a/8873922/514070">solution</a> working. I have seen people doing </p>
  
  <p><code>.encode!('UTF-8', 'UTF-8', :invalid =&gt; :replace)</code> </p>
  
  <p>but it doesnt appear to work with <code>File.readlines</code>.</p>
  
  <p><code>File.readlines('log.csv').encode!('UTF-8', 'UTF-8', :invalid =&gt; :replace).grep(/watch\?v=/)</code></p>
  
  <p><b>' : undefined method `encode!' for # (NoMethodError)</b></p>
  
  <p>Whats the most straightforward way to filter/convert invalid UTF-8 characters during a File read?</p>
  
  <p><strike>
  <b>Attempt 1</b></p>
  
  <p>Tried this but it failed with same invalid byte sequence error.</p>
  
  <pre><code>IO.foreach('test.csv', 'r:bom|UTF-8').grep(/watch\?v=/).map do |s|
    # extract three columns: time stamp, url, ip
    s = s.parse_csv;
    { timestamp: s[0], url: s[1], ip: s[3] }
  end
  </code></pre>
  
  <p></strike></p>
  
  <p><b>Solution</b></p>
  
  <p>This seems to have worked for me.</p>
  
  <pre><code>a = File.readlines('log.csv', :encoding =&gt; 'ISO-8859-1').grep(/watch\?v=/).map do |s|
  s = s.parse_csv;
  { timestamp: s[0], url: s[1], ip: s[3] }
  end
  puts a
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/11664403/does-ruby-provide-a-way-to-do-file-read-with-specified-encoding">Does Ruby provide a way to do File.read() with specified encoding?</a></p>