导出文件中的^ M个字符,转换为换行符 [英] ^M characters in exported file, converting to newlines
问题描述
我从excel导出了CSV以便使用python进行解析.当打开vimm格式的CSV时,我注意到所有换行符都应包含^ M个字符.
I exported a CSV from excel to parse using python. When I opened the vimmed the CSV, I noticed that it was all one line with ^M characters where newlines should be.
Name, Value, Value2, OtherStuff ^M Name, Value, Value2, OtherStuff ^M
我已解析文件,以便修改值并将其放入字符串(在csvreader中使用"rU"模式).但是,字符串没有换行符.所以我想知道,是否有办法在这个^ M字符上分割字符串,或者用\ n替换它?
I have the file parsed such that I modify the values and put the into a string (using 'rU' mode in csvreader). However, the string has no newlines. So I am wondering, is there a way to split the string on this ^M character, or a way to replace it with a \n?
推荐答案
^ M
是vim显示窗口行尾的方式
^M
is how vim displays windows end-of-line's
dos2unix
命令应为您解决这些问题:
The dos2unix
command should fix those up for you:
dos2unix my_file.csv
这是由于Windows/Unix上的EOL格式不同.
It's due to the different EOL formats on Windows/Unix.
在Windows上,它是 \ r \ n
On windows, it's \r\n
在Unix/Linux/Mac上,它只是 \ n
On Unix/Linux/Mac, it's just \n
^ M
实际上是vim,向您显示Windows CR(回车)或 \ r
The ^M
is actually vim showing you the windows CR (Carriage Return) or \r
python open
命令文档提供了有关处理通用换行符的更多信息: http://docs.python.org/2/library/functions.html#open
The python open
command documentation has more information on handling Universal Newlines: http://docs.python.org/2/library/functions.html#open
这篇关于导出文件中的^ M个字符,转换为换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!