4

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

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?

askewchan
  • 45,161
  • 17
  • 118
  • 134
corvid
  • 10,733
  • 11
  • 61
  • 130

2 Answers2

3

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

The dos2unix command should fix those up for you:

dos2unix my_file.csv

It's due to the different EOL formats on Windows/Unix.

On windows, it's \r\n

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

The ^M is actually vim showing you the windows CR (Carriage Return) or \r

The python open command documentation has more information on handling Universal Newlines: http://docs.python.org/2/library/functions.html#open

secretmike
  • 9,814
  • 3
  • 33
  • 38
1

If you are on a unix system, there is a program called dos2unix (and its counterpart unix2dos) that will do exactly that conversion.

But, it is pretty much the same as something like this:

sed -i -e 's/$/\r/' file
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
aet
  • 7,192
  • 3
  • 27
  • 25