I have a plain text file of tab-separated values that was created on a Windows machine. I want to import it into Mathematica on a Mac.

rawdata = Import["filename.csv", "tsv"]

The output list has a blank list every other element. I think this is due to the old \r\n-vs-\n compatibility issue. How can I tell Import which convention the input file is using?

Edit: I should mention that I want to do this in a cross-platform way, because I'll probably be moving notebooks between Mac and Windows machines. So simply skipping every other element of the output list ([[1;2;-1]]) won't work.

Edit 2: This is what the file looks like when I open it in Emacs:

4.510000000000000E+008  -1.052316667849458E+002 -1.412043034275902E+002 ^M
4.515000000000000E+008  -1.159523802353636E+002 1.631532615796773E+002 ^M

The spaces between the fields are tabs. A moment's Googling tells me that ^M is a carriage return (\r). This fits my hypothesis that the Windows-based device is terminating lines with \r\n, and Mathematica is reading that as two new-lines. My question remains: How do I tell Import what EOL characters are in use in my file?

  • $\begingroup$ Why the file name extension is .csv while the format of the file is TSV? The Import must not depend on this but it is strange. Which Mathematica version are you using? $\endgroup$ – Alexey Popkov Jan 16 '13 at 17:37
  • $\begingroup$ @belisarius I did. @AlexeyPopkov it was generated by a scientific instrument, and that's the format it came in. csv files are just plain text anyway. $\endgroup$ – poorsod Jan 16 '13 at 22:31
  • $\begingroup$ @poorsod - check the HEX of the end-of-line with an editor like Vim. Regular CSV end-of-line should be OdOa (as in CR/LF or \r\n). Perhaps you have something else. $\endgroup$ – Chris Degnen Jan 17 '13 at 9:25
  • $\begingroup$ @ChrisDegnen, please see my edit. $\endgroup$ – Benjamin Hodgson Jan 17 '13 at 17:46

It looks like that "IgnoreEmptyLines" might do the trick:

Mathematica graphics

  • $\begingroup$ Thank you! This cleared it right up. $\endgroup$ – Benjamin Hodgson Jan 19 '13 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.