2

I have a TXT data file (file.txt) which is formatted like so:

Column A (foo)       Column B             Column C                 
1.315116193802095950 6.564596662766380986 1.000428132834134232
...

Note that there are no tabs (all spaces), but all of the data is exactly the same column width in characters. Import["file.txt"] just spits back the text as is (super useful, thanks Mathematica!). Import["file.txt","Data"] will split the data line by line, but it's still just raw text within each line.

What would be great was an option that was present in good ole' Excel, which is to split the file at defined character widths. How do I get Mathematica to do this?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368

2 Answers2

1

What would be great was an option that was present in good ole' Excel, which is to split the file at defined character widths. How do I get Mathematica to do this?

In general you need StringTake which accepts a list of ranges as the second argument:

file = "Column A (foo)       Column B             Column C                 
  1.315116193802095950 6.564596662766380986 1.000428132834134232";
lines = StringSplit[file, "\n"];

data = StringTake[lines, {{1, 20}, {22, 41}, {43, 62}}]
Grid[data, Frame -> All]
{{"Column A (foo)      ", "Column B            ", "Column C            "},     
 {"1.315116193802095950", "6.564596662766380986", "1.000428132834134232"}}

output

You can convert strings representing numbers into actual numbers using NumberString and ToExpression:

Replace[data, 
 s_String /; StringMatchQ[StringTrim[s], NumberString] :> ToExpression[s], {-1}]
{{"Column A (foo)      ", "Column B            ", "Column C            "},
 {1.31511619380209595`18.118964125523707, 6.564596662766380986`18.817208047635546, 
 1.000428132834134232`18.00018589593615}} 
Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
0

Description

In order to avoid overcomplecating the problem at hand. I'd recommend to do some of the work partially e.g extracting headers. Below example demonstrated how to extract numberical data from the imported file and combine it with manually extracted headers into a table structure.

Example

row = "column (foo) column b column c 1.351 6.56 1.0005"

headers = {"column1", "column2", "column3"};
data = StringCases[row, x : NumberString :> ToExpression[x]];

Grid[{headers, data}]

Output

example

e.doroskevic
  • 5,959
  • 1
  • 13
  • 32