matlab - How to import a text file and prep for plotting and analysis with Python -
this first attempt of migrating matlab python 3.4 using mac os yosemite. i'm stuck simple issue, , here is:
inside folder, have multiple files different headers , data. of them have structure shown in below's example. perform these operations:
- remove lines start '#', such leave table of numbers in column format
- take n-th column of , plot y-axis vs. m-th column.
- take n-th column of certain file, , plot y-axis vs. m-th column of another file.
- take n-th column of several files, , plot y-axis vs. m-th column of 1 of them
i know question rather general, simple in matlab using "import" command (which reads header , data), imagine should simple in python. can't show i've tried, since attempt unsuccessful. appreciate anyone's help.
here file structure:
#s 3 #d #t 1 #q 0 0 24.0056 #p0 96.3973 53.9652 2.846164 0 10.159824 -0.1745831 -12.699097 70.863561 #p1 0.72284528 -0.62815186 -1.9 78.79958 7.8 61.3002 1.2075 -1.000035 #p2 3.3 10 9.46 0 0 2.1 -1.11 -1.605 #p3 0.25 -0.3 -0.22 -1.13 -0.05 -0.65 -0.28 -2.5 #p4 0.575 -1.475 3.404 3.204 2.86 1.86 -0.355 -0.655 #p5 6.45 5.95 1.99998 0.500066 1.99998 -0.299943 0.75 -0.29 #p6 0.76 -0.31 -0.115 -0.4 -0.341875 #ue 11.2115 1.10587 11.9701 11.2748 #n 19 #l chi h k l degk reg_degk field current epoch seconds iaps ionch2 2.5461672 -0.0009333 -0.0588542 24.0053 4.957 4.7046 -0.0006 0.0004 1244.977 1 101.7462 152960 2504 195506 187083 39896 272 30537 814 2.5661659 -0.000871762 -0.0549309 24.0053 4.9566 4.7046 0 -0.0002 1248.741 1 101.7148 152505 2484 195454 187013 39586 273 30299 805 2.5861645 -0.000810128 -0.0510075 24.0054 4.9568 4.7045 -0.0007 -0.0022 1252.477 1 101.7028 152727 2478 195607 187172 39711 273 30391 809 2.6061631 -0.000748396 -0.0470841 24.0054 4.9569 4.7047 0.0022 0.002 1256.208 1 102.202 152328 2478 195446 187022 39438 272 30182 799 2.6261617 -0.000686567 -0.0431607 24.0054 4.9569 4.7045 0.0009 0.0004 1259.944 1 102.201 152605 2495 195405 186963 39760 272 30422 874 2.6461604 -0.000624641 -0.0392373 24.0055 4.9566 4.7051 0.0004 -0.001 1263.685 1 102.198 152577 2491 195396 186994 39675 272 30356 900 2.666159 -0.000562619 -0.0353139 24.0055 4.9576 4.7054 0 -0.0002 1267.430 1 102.1768 152554 2483 195651 187219 39622 272 30325 826 2.6861576 -0.000500499 -0.0313905 24.0055 4.9569 4.7045 0.0017 -0.001 1271.155 1 102.1342 152685 2490 195365 186945 39775 272 30443 833 #r 11 4.9547 (scan nr., y_peak, @ pos., fwhm, @ pos., com, sum, time, temp_cs)
well starters, if lines not start #, snippet of code by.
with open("somefile.txt", "r") indata: # opens , reads file line in indata.readlines(): # splits file lines if not line.startswith("#"): # if line doesn't start # print print line
since used statement open , read file not have explicitly close file. statement handle that. next, if wanted create new file have lines removed this. note first statement open file writing name passed it. great 1 file, not if have iterate on many files.
with open('someoutfile', 'w') outdata: open("somefile.txt", "r") indata: line in indata.readlines(): if not line.startswith("#"): outdata.write(line)
lastly, here snippet iterate on folders contents , create new files formatting require
import os def get_file_names(folder): """yields 1 file @ time outfiles folder""" in os.listdir(folder): #pass name of directory or path directory yield folder_files_live_in = "somedirectory" filename in get_file_names(folder_files_live_in): outfile = "outfile" count = 1 # gives output files level of uniqueness open(outfile + str(count) , 'w') outdata: open(folder_files_live_in + '/' + filename, "r") indata: #the somedirectory same directory passed in get_file_names() function line in indata.readlines(): if not line.startswith("#"): outdata.write(line) count += 1
Comments
Post a Comment