即使两个字符串完全相同的编码,也会出现不兼容的编码

时间:2012-10-10 21:48:46

标签: ruby

我正在尝试将文件中的内容追加到字符串中。

使用iso-8859-1编码打开文件并将字符串设置为相同的编码。

但在尝试连接值时,我收到incompatible character encodings: UTF-8 and ISO-8859-1 (Encoding::CompatibilityError)错误。

如果两个字符串具有相同的编码,为什么会发生这种情况?

实际上我没有UTF-8字符串。

sql = "
INSERT INTO pages
(meta_title, meta_description, meta_keywords, title, URL, content)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s');
".force_encoding('iso-8859-1') # setting string to iso-8859-1

Dir['./*'].select { |e| File.file? e }.each do |e|
  f = File.open(e, "r:iso-8859-1") # opening the file using iso-8859-1
  # extracting meta, title etc
  puts sql % [*meta, title, url, content]
end

0 个答案:

没有答案
相关问题