Python : How to read through columns in text files? -


i have text file delimited space, looks below

title 1 589.890 0.260 fine 0.100 lex larry_page " " " title 1 590.150 0.000 . 0.950 lex larry_page " " " title 1 592.290 0.130 here 0.990 lex larry_page " " " title 1 592.420 0.160 0.990 lex larry_page " " " title 1 592.580 0.280 go 0.990 lex larry_page " " " title 1 592.860 0.000 , 0.100 lex larry_page " " " title 1 593.180 0.270 taking 0.990 lex larry_page " " " title 1 593.450 0.170 russel 0.990 lex larry_page person_name russel_arnold object title 1 593.640 0.060 0.990 lex larry_page " " " title 1 593.700 0.110 0.990 lex larry_page " " " title 1 593.810 0.460 team 0.990 lex larry_page " " " title 1 594.270 0.000 . 0.950 lex larry_page " " " title 1 594.920 0.140 in 0.990 lex larry_page " " " title 1 595.060 0.090 0.990 lex larry_page " " " title 1 595.150 0.360 sack 0.990 lex larry_page " " " title 1 595.510 0.000 . 0.950 lex larry_page " " " title 1 598.810 0.360 hey 0.100 lex larry_page " " " title 1 599.170 0.460 helen 0.990 lex larry_page person_name helen_winkle addressee title 1 599.630 0.000 . 0.950 lex larry_page " " " title 1 600.490 0.170 hi 0.530 lex helen_winkle " " " title 1 600.740 0.290 guys 0.530 lex helen_winkle " " " title 1 601.030 0.000 . 0.950 lex helen_winkle " " " title 1 602.010 0.220 helen 0.990 lex larry_page person_name helen_winkle addressee title 1 602.230 0.000 , 0.100 lex larry_page " " " title 1 602.280 0.140 0.990 lex larry_page " " " title 1 602.470 0.100 have 0.990 lex larry_page " " " title 1 602.600 0.030 0.990 lex larry_page " " " title 1 602.950 0.350 question 0.990 lex larry_page " " " title 1 603.300 0.180 0.990 lex larry_page " " " title 1 603.480 0.190 0.990 lex larry_page " " " title 1 603.670 0.000 , 0.100 lex larry_page " " " title 1 603.670 0.060 , 0.990 lex larry_page " " " title 1 603.730 0.070 0.990 lex larry_page " " " title 1 603.840 0.180 might 0.990 lex larry_page " " " title 1 604.020 0.200 0.990 lex larry_page " " " title 1 604.220 0.460 0.100 lex larry_page " " " title 1 604.680 0.170 little 0.990 lex larry_page " " " title 1 604.850 0.550 awkward 0.990 lex larry_page " " " title 1 605.400 0.000 , 0.100 lex larry_page " " " title 1 605.610 0.090 0.990 lex larry_page " " " title 1 605.700 0.320 know 0.990 lex larry_page " " " title 1 606.020 0.000 , 0.100 lex larry_page " " " title 1 606.340 0.260 given 0.990 lex larry_page " " " title 1 606.660 0.130 0.990 lex larry_page " " " title 1 606.870 0.330 0.990 lex larry_page " "  

here i'm trying reformat on basis of column[7]

start_time end_time column[7] column[9]names  

i'm trying reformat text :

589.890 599.630 larry_page russel_arnold helen_winkle    600.490 601.030 helen_winkle " 602.010 607,200 larry_page helen_winkle  

in above format helen_winkle has no names in column[9] gave "

ps : sometimes, may have more names below

589.890 599.630 larry_page russel_arnold helen_winkle jerome_halloy leo_cazenille 

i stuck here , don't have idea how proceed further

path = "path of textfile" open(path,'r') f :         line in f:             columns = line.strip().split()             start = float(columns[2])             end = start+float(columns[3])             pro_name = columns[9]             s_name = columns[7] 

what you're trying part of more general problem called "corner turning" - rows in, columns out. it's difficult problem efficiently, though data numerical when read in academic papers.

in case, i'd create empty lists, append values them during for-loop.

path = "path of textfile" start_list = [] end_list = [] pro_name_list = [] s_name_list = [] open(path,'r') f :     line in f:         columns = line.strip().split()         start = float(columns[2])         start_list.append(start)         end = start+float(columns[3])         end_list.append(end)         pro_name = columns[9]         pro_name_list.append(pro_name)         s_name = columns[7]         s_name_list.append(s_name) 

edit: re-read question , realised hadn't answered properly... think once have arrays representing columns, can re-create file whatever order need to? did understand asked correctly?


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -