Search code examples
pythonsplitline-breaks

Split string and append parts in running list [Python]


I have a veery long list that contains the same pattern. Here an original example:

04:50 10 244.685 0 0
05:00 10 244.680 0 0
HBCHa 9990 Seite 762
16.02.16
Hafenpegel 
Pegel müM Var 0 Pegelstand
Datum Zeit Intervall müM Q Art
Sonntag, 31. Dezember 2000 05:10 10 244.679 0 0
05:20 10 244.688 0 0
05:30 10 244.694 0 0
05:40 10 244.688 0 0

As you can see, there is one line with measurement-data within the string that starts with "Sonntag"

My target is:

04:50 10 244.685 0 0
05:00 10 244.680 0 0
HBCHa 9990 Seite 762
16.02.16
Hafenpegel 
Pegel müM Var 0 Pegelstand
Datum Zeit Intervall müM Q Art
Sonntag, 31. Dezember 2000 
05:10 10 244.679 0 0            !!
05:20 10 244.688 0 0
05:30 10 244.694 0 0
05:40 10 244.688 0 0

I managed to write the txt-file in a list, here called "data_list_splitted", catch this onle line over the whole txt-file, split it and extract the part with the measurements:

for i in data_list_splitted:

if len(i) >= 40:

    ii = i.split(";")


    txt_line = "%s;%s;%s;%s;%s"%(ii[4],ii[5],ii[6],ii[7],ii[8])

But i don't get it to break this line and add the measurement-values in the running list!

I think this should't be that difficult? Any ideas? Thank you very much!


Solution

  • You can create another list and insert values into it

    new_data_list_splitted = []
    for i in data_list_splitted:
      if len(i) >= 40:
        ii = i.split(";")
        txt_line = "%s;%s;%s;%s;%s"%(ii[0],ii[1],ii[2],ii[3],ii[4])
        new_data_list_splitted.append(txt_line)
        txt_line = "%s;%s;%s;%s;%s"%(ii[4],ii[5],ii[6],ii[7],ii[8])
        new_data_list_splitted.append(txt_line)
      else:
        new_data_list_splitted.append(i)
    
    print new_data_list_splitted #this will have a new row for measurement value