Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have files that look like this:

8853,548600 39,021824   
8143,632600 37,200790   
7815,964200 35,926544   
7106,337000 34,761822   
6724,260400 33,491154   
6178,100200 32,254318

I want to import it and then change the decimal point from , to . and convert it so that it has the the right head (so I can manipulate it like a number).

My attempt that doesn't work:

data = Apply[StringReplace[#, {"," -> "."}] &, 
  Import[file, "Data"], {2}]
share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

For several reasons I recommend that you do not use VLC's method. First, it is going to be slower than interpreting the data during import, and second it creates the possibility of unintentionally running code via ToExpression.

The most direct and robust method is to specify "NumberPoint" in Import:

Import["file.txt", "Table", "NumberPoint" -> ","]
{{8853.5486, 39.021824}, 
 {8143.6326, 37.20079}, 
 {7815.9642, 35.926544}, 
 {7106.337,  34.761822}, 
 {6724.2604, 33.491154}, 
 {6178.1002, 32.254318}}

For faster importing, at the risk of crashing Mathematica on irregular data, you may make use of the undocumented internal function described here which is actually the subroutine used by Import without all the slow dressing used to auto-detect formats.

table = ReadList["file.txt", Word, RecordLists -> True];

System`Convert`TableDump`ParseTable[table, {{{}, {}}, {"-", "+"}, ","}, False]
{{8853.5486, 39.021824}, 
 {8143.6326, 37.20079}, 
 {7815.9642, 35.926544}, 
 {7106.337,  34.761822}, 
 {6724.2604, 33.491154}, 
 {6178.1002, 32.254318}}
share|improve this answer
    
Good points, never thought about the possibility that some code could be unintentionally executed. I'm going to delete my answer, so nobody will be tempted to use that approach. –  VLC Nov 9 '12 at 10:36
    
@VLC Please don't; it's a good reference and it illustrates a way to make the method that the OP attempted work. I just think in this case it is not practical. –  Mr.Wizard Nov 10 '12 at 21:25
add comment

This should solve your problem:

ToExpression[StringReplace[#, "," -> "."]] & /@ 
  Import[file, "Table"]
share|improve this answer
    
@Althalos You might reconsider your choice of accepted answer (see Mr.Wizard's answer). –  VLC Nov 9 '12 at 11:35
add comment

This will do the job on Linux, or if you have Linux utils installed on Windows.

data= ReadList["!sed 's/,/\./g' /tmp/data.txt", {"Number", "Number"}]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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