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');
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.