去除文件头部的u+feff_关于FEFF的简短故事,一个不可见的UTF-8字符破坏了我们的CSV文件

去除文件头部的u+feff

Today, we encountered an error while trying to create some database seeds from a CSV. This CSV was originally generated by me using a Ruby script which piped the output to a file and saved as a CSV.

今天,我们在尝试从CSV创建一些数据库种子时遇到错误。 该CSV最初是由我使用Ruby脚本生成的,该脚本将输出通过管道传输到文件并另存为CSV。

The CSV was checked in to Git and had been used for awhile until we had to update some parts of it by adding a new column and fixing some values.

CSV已签入Git,并使用了一段时间,直到我们不得不通过添加新列并修复一些值来更新其中的某些部分。

While we don’t know the exact reason yet, my theory is that somehow, Excel for Mac (we are all using Macs) added some additional metadata to it even after saving the file as a CSV.

尽管我们尚不知道确切原因,但我的理论是,即使将文件另存为CSV,Excel for Mac(我们都在使用Mac)也向其中添加了一些其他元数据。

This in turn made anyone using the seed receive the following error:

反过来,这使使用种子的任何人都收到以下错误:

CSV::MalformedCSVError: Illegal quoting in line 1.

I opened the CSV file and nothing looked suspicious. My first thought was some left/right quotation marks were somehow mixed into the file instead of just the ‘normal’ double quotes: ". But upon further investigation, there was nothing out of the ordinary. This led me to just wipe out the whole file, and actually type out the first row again.

我打开了CSV文件,但没有任何可疑的地方。 我首先想到的是,文件中混入了一些左/右引号,而不仅仅是“正常”双引号: " 。但是,经过进一步的调查,发现并没有什么不寻常的地方。这导致我只消了整个内容。文件,然后再次键入第一行。

I saved that file again and ran the migration:

我再次保存该文件并运行迁移:

CSV::MalformedCSVError: Illegal quoting in line 1.

What?!

什么?!

Okay, this was driving me nuts. I opened up a new file, typed the exact single line again, and ran the migration. It worked. So what was in that file?!

好吧,这真让我发疯。 我打开了一个新文件,再次键入了确切的单行,然后运行了迁移。 有效。 那那个文件里有什么?

Only one way to find out:

只有一种方法可以找出:

cat companies.csv | pbcopy | pbpaste > temp.csv
rm companies.csv
mv temp.csv companies.csv
git diff

So OSX has these two functions that are very useful: pbcopy and pbpaste. Basically anything piped to pbcopy gets into your clipboard and pbpaste puts what you have on your clipboard to standard output (stdout). But it removes all formatting.

因此OSX具有这两个非常有用的功能: pbcopypbpaste 。 基本上,通过管道传输到pbcopy都会进入剪贴板,而pbpaste会将剪贴板上的pbpaste放入标准输出(stdout)。 但是它将删除所有格式。

Very useful when you want to just copy some text from somewhere and you want to paste it into a WYSIWYG editor without all the formatting. Like when writing an email from Gmail, for example.

当您只想从某处复制一些文本并将其粘贴到WYSIWYG编辑器而不使用所有格式时,此功能非常有用。 例如,从Gmail编写电子邮件时。

I then removed the original file and saved the new ‘unformatted’ file with the same file name so I could see the difference.

然后,我删除了原始文件,并使用相同的文件名保存了新的“未格式化”文件,这样我就可以看到区别。

And we finally saw the invisible man:

最后我们看到了那个看不见的人:

A quick Google search told us that our friend U+FEFF was called a ZERO WIDTH NO-BREAK SPACE. Also, a quick trip to Wikipedia told us about the actual uses for U+FEFF, more commonly known as Byte order mark or BOM.

快速的Google搜索告诉我们,我们的朋友U+FEFF被称为ZERO WIDTH NO-BREAK SPACE 。 另外, 快速访问Wikipedia告诉了我们U+FEFF的实际用法,通常被称为Byte order markBOM

Our friend FEFF means different things, but it’s basically a signal for a program on how to read the text. It can be UTF-8 (more common), UTF-16, or even UTF-32.

我们的朋友FEFF意味着不同的事情,但这基本上是一个程序如何阅读文本的信号。 它可以是UTF-8 (更常见), UTF-16甚至UTF-32

FEFF itself is for UTF-16 — in UTF-8 it is more commonly known as 0xEF,0xBB, or 0xBF.

FEFF本身是针对UTF-16 -在UTF-8它通常被称为0xEF,0xBB, or 0xBF

From my understanding, when the CSV file was opened in Excel and saved, Excel created a space for our invisible stowaway, U+FEFF. And in front of the file to boot!

据我了解,当在Excel中打开并保存CSV文件时,Excel为我们的隐形U+FEFF创建了一个空间。 并在文件前面启动!

Excel did some magic, and it was probably saved in UTF-16 instead of UTF-8. UTF-8 does not understand BOM and just treats it as a non-character so visually, the file was okay. But Ruby’s CSV thought that there was something wrong because it assumed the file it was reading was UTF-8 and it couldn’t ignore Mr. U+FEFF.

Excel做了一些魔术,它可能保存在UTF-16而不是UTF-8UTF-8不了解BOM而只是将其视为非字符,因此从视觉上看,该文件还可以。 但是Ruby的CSV认为出了点问题,因为它假定正在读取的文件是UTF-8 ,并且不能忽略U+FEFF先生。

So lesson learned: don’t open (and save!) a CSV file in Excel if you want to feed it to Ruby’s CSV parser.

因此,我们汲取了教训:如果您想将其馈送到Ruby的CSV解析器中,请不要在Excel中打开(并保存!)CSV文件。

If you do ever encounter an error like that, be sure to look for hidden characters not shown by your editor. If you still can’t see it and are using OSX, then pbcopy and pbpaste will help you out — they strip out any formatting or hidden characters from text in addition to copying and pasting it.

如果您确实遇到过这样的错误,请确保查找编辑器未显示的隐藏字符。 如果您仍然看不到它并使用OSX,则pbcopypbpaste将为您提供帮助-除了复制和粘贴外,它们还会从文本中删除所有格式或隐藏字符。

翻译自: https://www.freecodecamp.org/news/a-quick-tale-about-feff-the-invisible-character-cd25cd4630e7/

去除文件头部的u+feff

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393941.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis——学习之路一(初识redis)

在接下来的一段时间里面我要将自己学习的redis整理一遍,下面是我整理的一些资料: Redis是一款依据BSD开源协议发行的高性能Key-Value存储系统(cache and store),所以redis是可以查看源代码https://github.com/MSOpenTe…

matlab 处理dat文件画图,matlab_DAT_processing matlab处理dat文件并进行绘图 - 下载 - 搜珍网...

matlab实验2/11.txtmatlab实验2/B00001.datmatlab实验2/B00002.datmatlab实验2/B00003.datmatlab实验2/B00004.datmatlab实验2/B00005.datmatlab实验2/B00006.datmatlab实验2/B00007.datmatlab实验2/corv.txtmatlab实验2/cory.txtmatlab实验2/matlab批量载入数据.txtmatlab实验…

leetcode面试题 08.03. 魔术索引(二分)

魔术索引。 在数组A[0…n-1]中,有所谓的魔术索引,满足条件A[i] i。给定一个有序整数数组,编写一种方法找出魔术索引,若有的话,在数组A中找出一个魔术索引,如果没有,则返回-1。若有多个魔术索引…

python返回序列中的最小元素_python实现获取序列中最小的几个元素

本文实例讲述了python实现获取序列中最小的几个元素。分享给大家供大家参考。具体方法如下:import heapqimport randomdef issorted(data):data list(data)heapq.heapify(data)while data:yield heapq.heappop(data)alist [x for x in range(10)]random.shuffle(a…

apache访问快捷方式

<VirtualHost *:80>   DocumentRoot "XXX"   ServerName XXX   Alias /pdodata/ "XXX"</VirtualHost> 其中 Alias /pdodata/ "XXX" /data/是快捷方式名称 后面的代表快捷方式具体目录名称转载于:https://www.cnblogs.com/…

css----实现checkbox图片切换

1、效果图 2、代码 <!DOCTYPE html> <html><head><meta charset"utf-8"><title>checkbox</title><style type"text/css">label {width: 20px;font-size: 12px;cursor: pointer;}label i {display: inline-block…

Node.js 究竟是什么?

在网上看到一篇介绍Node.js的文章&#xff0c;很好的介绍了Node.js Michael Abernethy, 自由程序员, Freelancer 2011 年 10 月 09 日 (最初于 2011 年 4 月 26 日) Node.js 究竟是什么&#xff1f; 一个 “编码就绪” 服务器 Node 是一个服务器端 JavaScript 解释器&#xff…

react中绑定点击事件_在React中绑定事件处理程序的最佳方法

react中绑定点击事件by Charlee Li通过李李 在React中绑定事件处理程序的最佳方法 (The best way to bind event handlers in React) Binding event handlers in React can be tricky (you have JavaScript to thank for that). For those who know the history of Perl and P…

json_decode php数组,json_decode转化为数组加true,json_encode和json_decode区别

一、json_encode和json_decode区别1、json_encode&#xff1a;对象/数组 ---> json2、json_decode&#xff1a;json ---> 对象/数组二、json_decode转化为数组转化为数组时&#xff0c;第二个参数很重要&#xff1a;不加true会以PHP对象输出, 加true输出PHP数组&#xff…

leetcode1219. 黄金矿工(回溯)

你要开发一座金矿&#xff0c;地质勘测学家已经探明了这座金矿中的资源分布&#xff0c;并用大小为 m * n 的网格 grid 进行了标注。每个单元格中的整数就表示这一单元格中的黄金数量&#xff1b;如果该单元格是空的&#xff0c;那么就是 0。 为了使收益最大化&#xff0c;矿工…

【无删减】Python老司机收藏夹的17个国外免费学习网站

用Python编写代码一点都不难&#xff0c;事实上它一直被赞誉为最容易学的编程语言。如果你准备学习web开发&#xff0c; Python是一个不错的开始&#xff0c;甚至想做游戏的话&#xff0c;用Python来开发游戏的资源也有很多。这是快速学习这门语言的途径之一。许多程序员都把Py…

iframe vue 前进 后退_vue常见面试题

1、说说你对 SPA 单页面的理解&#xff0c;它的优缺点分别是什么&#xff1f;SPA&#xff08; single-page application &#xff09;仅在 Web 页面初始化时加载相应的 HTML、JavaScript 和 CSS。一旦页面加载完成&#xff0c;SPA 不会因为用户的操作而进行页面的重新加载或跳转…

C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码

C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码 原文:C#编写运行在Linux环境下的采用Mediainfo来获取多媒体文件信息的代码项目开始设计的是运行在windows下&#xff0c;所以一开始采用的是windows服务模式来获取多媒体文件信息&#xff0c;后来要求调整为…

如何用chrome扩展将网页变成黑底白字,用以保护视力

不知道有没有科学依据&#xff0c;自己感觉黑底白字对视力好些&#xff0c;于是动手加个chrome扩展&#xff1a; 第一步&#xff1a;建个文件夹&#xff0c;名称比如叫changeColor; 第二步&#xff1a;在changeColor文件夹中建三个文件&#xff1a;manifest.json 、 backgrou…

从零学习机器学习_机器学习:如何从零变英雄

从零学习机器学习以“为什么&#xff1f;”开头 并以“我准备好了&#xff01;”结尾 (Start with “Why?” and end with “I’m ready!”) If your understanding of A.I. and Machine Learning is a big question mark, then this is the blog post for you. Here, I gradu…

sqoop动态分区导入mysql,使用sqoop import从mysql往hive含分区表中导入数据的一些注意事项...

先看下面这条语句&#xff0c;它实现的功能是将特定日期的数据从mysql表中直接导入hive$ sqoop import \--connect jdbc:mysql://192.168.xx.xx:3306/db_name?useSSLfalse \--username xxx --password xxxxxx \--query "select d.id, d.callsign, d.sobt from t_flight_b…

leetcode面试题 08.04. 幂集(递归)

幂集。编写一种方法&#xff0c;返回某集合的所有子集。集合中不包含重复的元素。 说明&#xff1a;解集不能包含重复的子集。 示例: 输入&#xff1a; nums [1,2,3] 输出&#xff1a; [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 代码 class Solution {List&l…

gatsby_我如何使用Gatsby和Netlify建立博客

gatsbyby Pav Sidhu通过帕夫西杜(Pav Sidhu) 我如何使用Gatsby和Netlify建立博客 (How I Built My Blog Using Gatsby and Netlify) 您能说出更具标志性的二人​​组合吗&#xff1f; &#xff1f; (Can you name a more iconic duo? ?) Years ago, whenever I built a stat…

交叉熵与相对熵

熵的本质是香农信息量()的期望。 现有关于样本集的2个概率分布p和q&#xff0c;其中p为真实分布&#xff0c;q非真实分布。 按照真实分布p来衡量识别一个样本的所需要的编码长度的期望(即平均编码长度)为&#xff1a;H(p)。 如果使用错误分布q来表示来自真实分布p的平均编码长度…

menustrip

在对应菜单上点击鼠标右键&#xff0c;插入&#xff0c;SEPARATOR 就可以了&#xff0c;然后可以选中拖动位置。转载于:https://www.cnblogs.com/Echo529/p/6382302.html