0

I have the following ORIGINAL.TXT file:

"TRACE: A"
"FORMAT: WOW"

"Frequency" "Data 1"    "Data 2"
1   1   2
1   6   0

"Frequency" "Data 1"    "Data 2"
1   5   0
1   6   0

In order to run python script, I have to first open .TXT file in LibreOffice, then save and close it, to create EXPECTED.TXT file with a delimiter=',', as shown below:

"TRACE: A",,
"FORMAT: WOW",,
,,
"Frequency","Data 1","Data 2"
1,5,0
1,5,0

"Frequency","Data 1","Data Trace Imag"
1,5,0
1,5,0

Now I can use the following code to plot the graphs:

import numpy as np
from pylab import *

fileName = 'EXPECTED.TXT';

traceAdata= np.genfromtxt(fileName, delimiter=',')[3:5].T;

x = traceAdata[0]*1e-6;
A = traceAdata[1];

plot(x,A);
savefig(fileName + '.png');

I have tried to use different delimiters in the original TXT file such as: ' ', None, '\t', but none of them worked, and I had following error:

Line #5 (got 3 columns instead of 1)

Is there any other way to convert original TXT file to the file with needed delimiter using python?

UPD: Current Solution, thank you @lxop.

import numpy as np
from pylab import *

fileName = 'C2.TXT';

traceAdata= np.genfromtxt(fileName, skip_header=21,skip_footer = 334).T;

x = traceAdata[0]*1e-6;
A = traceAdata[1];

plot(x,A);
savefig('traceAdata.png');

traceBdata= np.genfromtxt(fileName, skip_header=337).T;
x = traceBdata[0]*1e-6;
B = traceBdata[1];

plot(x,B);
savefig('traceBdata.png');
Random Data
  • 53
  • 1
  • 9
  • 2
    Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. – Machavity Jul 20 '18 at 12:42

1 Answers1

1

You can load the space separated file directly, without converting, with a script like

import numpy as np
from pylab import *

fileName = '11.TXT';

traceAdata= np.genfromtxt(fileName, skip_header=4).T;

x = traceAdata[0]*1e-6;
A = traceAdata[1];

plot(x,A);
savefig(fileName + '.png');

In particular not the use of skip_header to skip over the lines that don't have data. Also, if you explicitly specify delimiter=' ' then genfromtxt will treat each space as a delimiter and try to get values between the individual space characters, so just leave that argument out.

If you really want to convert it to a CSV first, then you should look at a parsing library, since it will need to handle quoting etc.

lxop
  • 7,596
  • 3
  • 27
  • 42
  • With a more complex file structure like you seem to have, you might be better off opening the file and reading out all the lines, separating them into data sets, then reading those data sets into numpy after that, rather than abusing the header/footer handling of `getfromtxt`. – lxop Nov 17 '17 at 12:07