来自 CSV 文件的字符串开头的神秘前导 "empty" 字符

mysterious leading "empty" character at beginning of a string which came from CSV file

在将 CSV 文件读入数组的过程中,我注意到第一个数组元素(字符串)包含前导 ""。

例如:

str = contacts[0][0]
p str

给我...

"SalesRepName"

然后我碰巧尝试了:

str = contacts[0][0].split(//)
p str

这给了我...

["", "S", "a", "l", "e", "s", "R", "e", "p", "N", "a", "m", "e"]

我检查了数组中的所有其他元素,这是唯一一个字符串包含前导“”的元素。

现在,在我 post 这个问题之前,我偶然发现了答案。显然,我写这个问题的行为让我想到了确定这个 "" 字符的 ascii 数字。

str = contacts[0][0].split(//)
p str[0].codepoints

给了我

[65279]

在查询 ascii 字符 65279 时,我找到了这篇文章:

根据 SLaks:

It's a zero-width no-break space. It's more commonly used as a byte-order mark (BOM).

这反过来又让我想到了这里的解决方案:
在这个响应中,knut 提供了一个优雅的解决方案,看起来像这样:

File.open('file.txt', "r:bom|utf-8"){|file|
  text_without_bom = file.read
}

、"r:bom|utf-8" 是我要找的关键元素。 所以我将它改编成我的代码,变成了这样:

CSV.foreach($csv_path + $csv_file, "r:bom|utf-8") do |row|
  contacts << row
end

我在这个愚蠢的问题上花了好几个小时。希望这能为您节省一些时间!