去除文件頭部的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.pswp.cn/news/393941.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393941.shtml
英文地址,請注明出處:http://en.pswp.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