当我从以下位置导出 LinkedIn 连接时:
https://www.linkedin.com/connected/manage_sources
我收到了一个 Microsoft Outlook CSV 文件。
但是当我尝试CSV.read
使用 Ruby 访问该文件时,出现以下错误:
invalid byte sequence in UTF-8
我可以通过在 Excel 中打开 CSV 然后Save As
使用 UTF-8 编码对其进行正确编码。
但是,我真的希望能够从命令行执行此操作,而根本不必使用 Excel。
我读到另一个答案这iconv
可能是一个选择。但我无法让它发挥作用:
iconv -f US-ASCII -t UTF-8 test/fixtures/1481995385116.csv
错误:
iconv: test/fixtures/1481995385116.csv:145:19: cannot convert
当我检查它是什么类型的文件时,我得到:
test/fixtures/1481995385116.csv: Non-ISO extended-ASCII text, with very long lines, with CRLF, LF line terminators
我可以使用其他 CLI 还是我使用iconv
错误?
编辑:
根据建议,输出hexdump
:
➜ c/t/fixtures master ✗ hexdump 1482372034326.csv|head
0000000 22 54 69 74 6c 65 22 2c 22 46 69 72 73 74 20 4e
0000010 61 6d 65 22 2c 22 4d 69 64 64 6c 65 20 4e 61 6d
0000020 65 22 2c 22 4c 61 73 74 20 4e 61 6d 65 22 2c 22
0000030 53 75 66 66 69 78 22 2c 22 45 2d 6d 61 69 6c 20
0000040 41 64 64 72 65 73 73 22 2c 22 45 2d 6d 61 69 6c
0000050 20 32 20 41 64 64 72 65 73 73 22 2c 22 45 2d 6d
0000060 61 69 6c 20 33 20 41 64 64 72 65 73 73 22 2c 22
0000070 42 75 73 69 6e 65 73 73 20 53 74 72 65 65 74 22
0000080 2c 22 42 75 73 69 6e 65 73 73 20 53 74 72 65 65
0000090 74 20 32 22 2c 22 42 75 73 69 6e 65 73 73 20 53
➜ c/t/fixtures master ✗ file 1482002728101.csv
1482002728101.csv: UTF-8 Unicode text, with very long lines, with CR line terminators
➜ c/t/fixtures master ✗ file 1482372034326.csv
1482372034326.csv: Non-ISO extended-ASCII text, with very long lines, with CRLF, LF line terminators
➜ c/t/fixtures master ✗ hexdump -c 1482002728101.csv|head
0000000 T i t l e , F i r s t N a m e
0000010 , M i d d l e N a m e , L a s
0000020 t N a m e , S u f f i x , E -
0000030 m a i l A d d r e s s , E - m
0000040 a i l 2 A d d r e s s , E -
0000050 m a i l 3 A d d r e s s , B
0000060 u s i n e s s S t r e e t , B
0000070 u s i n e s s S t r e e t 2
0000080 , B u s i n e s s S t r e e t
0000090 3 , B u s i n e s s C i t y
➜ c/t/fixtures master ✗ hexdump -c 1482372034326.csv|head
0000000 " T i t l e " , " F i r s t N
0000010 a m e " , " M i d d l e N a m
0000020 e " , " L a s t N a m e " , "
0000030 S u f f i x " , " E - m a i l
0000040 A d d r e s s " , " E - m a i l
0000050 2 A d d r e s s " , " E - m
0000060 a i l 3 A d d r e s s " , "
0000070 B u s i n e s s S t r e e t "
0000080 , " B u s i n e s s S t r e e
0000090 t 2 " , " B u s i n e s s S
如何从输出中辨别格式?
答案1
$ iconv -f windows-1252 -t utf-8 linkedin_contacts.csv
.
.
.
"","Ahmet XXXXX","","??
iconv: linkedin_contacts.csv:665:23: cannot convert
$ cat linkedin_contacts.csv|grep Ahmet|hexdump -C| sed -n '1,2p'
00000000 22 22 2c 22 41 68 6d 65 74 20 53 61 6c 69 68 22 |"","Ahmet XXXXX"|
00000010 2c 22 22 2c 22 3f 3f 8d 65 6e 22 2c 22 22 2c 22 |,"","??.en","","|
8d
我查了一下价值ASCII表看起来它是 ISO 8859-1 的变体。检查iconv --list | grep 8859-1
确认iconv
可以处理。
$ iconv -f ISO-8859-1 -t UTF-8 linkedin_contacts.csv > foo.rb
$ file foo.rb
foo.rb: UTF-8 Unicode text, with very long lines, with CRLF, LF line terminators
拥有这两个终结符对于 ruby 来说仍然是一个需要处理的问题,但如果我们砍掉末端,那么一切都很好:)
$ sed '$ d' foo.rb > bar.csv
$ file bar.csv
bar.csv: UTF-8 Unicode text, with very long lines, with CRLF line terminators