^ M characters in exported file, conversion to newlines

I exported CSV from excel for parsing using python. When I opened vimmed CSV, I noticed that it was one line with ^ M characters, which should contain newlines.

Name, Value, Value2, OtherStuff ^M Name, Value, Value2, OtherStuff ^M 

I have a file processed in such a way that I change the values ​​and put them in a string (using the "rU" mode in csvreader). However, the line has no new lines. So I'm wondering if there is a way to split the string into this ^ M character or a way to replace it with \ n?

+4
source share
2 answers

^M is how vim displays end-of-line windows

The dos2unix command should fix this for you:

 dos2unix my_file.csv 

This is due to the various EOL formats on Windows / Unix.

In windows it's \r\n

On Unix / Linux / Mac, it's just \n

^M actually vim shows you CR windows (carriage return) or \r

The python open command documentation contains more information about working with Universal Newlines: http://docs.python.org/2/library/functions.html#open

+3
source

If you are on a unix system, there is a program called dos2unix (and a copy of unix2dos ) that will perform this conversion.

But this is almost the same as something like this:

 sed -i -e 's/$/\r/' file 
+1
source

Source: https://habr.com/ru/post/1492362/


All Articles