First, from your discription I assume x1, x2, y1 and y2 below "经纬度" do not mean anything to you.
Let's suppose that the data in the picture you showed us is all you want and that the original data is formatted as the example (eg. there are only two data columns, namely val1 and val2; val1 and val2 always have 3 values per timestamp; val2 always comes after val1), then the following solution should work:
import re
#define 4 patterns
p1=r'time:\s*(\d+)' # for time: 201510010000
p2=r'\[([\d\.]+),([\d\.]+),([\d\.]+)\]' # for [1.1,2.1,3.1]
v1p=u'变量名:\s*val1' # for val1
v2p=u'变量名:\s*val2' # for val2
inV1=False # the flag to show if next line is for val1
inV2=False # the flag to show if next line is for val1
time_column=''
csv_f=open('output.csv','w',encoding='utf-8') #open a csv file for writing
csv_f.write('time,val1,val2')
with open('text.txt','r',encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
m=re.match(p1,line)
if m and time_column != m.groups()[0]:
time_column = m.groups()[0]
#reset the flags
inV1=False
inV2=False
continue
if re.match(v1p,line):
inV1=True
continue
if re.match(v2p,line):
inV2=True
continue
m=re.match(p2,line)
if not m: continue
if inV1:
val1=m.groups()
if inV2: # we should ouput all the values for a timestamp since both val2 and val1 are ready now
val2=m.groups()
for i in range(0,3):
l="{0},{1},{2}".format(time_column,val1[i],val2[i])
csv_f.write("\n"+l)
csv_f.close() #close the csv file
What the above code does is parse the given text and write the formatted output to a csv file named "output.csv" in the same folder as "text.txt". You can open it directly with MS Excel or any other spreedsheet editor or viewer.
I used regex here because it's most flexible and you could always modify the patterns to suit your needs without changing the remaining logics. Also using flags has the advantage of not being confused by possible duplicate lines in the text.
Should you have further requirements, please leave a comment.