我有一个.txt文件,如下所示:
# 经纬度
x1 = 11.21
x2 = 11.51
y1 = 27.84
y2 = 10.08
time: 201510010000
变量名: val1
[1.1,1.2,1.3]
变量名: va2
[1.0,1.01,1.02]
time: 201510010100
变量名: val1
[2.1,2.2,2.3]
变量名: va2
[2.01,2.02,2.03]
time: 2015020000
变量名: val1
[3.0,3.1,3.2]
变量名: val2
[3.01,3.02,3.03]
time: 2015020100
变量名: val1
[4.0,4.1,4.2]
变量名: val2
[401,4.02,4.03]
并且,我希望使用python这样阅读它:
with open('text.txt','r',encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
print(line,)
这是我所做的,但是我不知道下一步。
我如何到达它?
答案 0 :(得分:0)
我建议您更改.txt格式并转换为.ini文件或.csv。 无论如何,您可以使用字典。
dict = {}
file = open("file.txt")
text = file.readline()
i=0
for i in range (text.lenght):
if text[i][0:5]=="time":
dict[text[i]] = []
dict[text[i]].append(text[i+2])
dict[text[i]].append(text[i+4])
该代码可能适用于您的文件,但是如果您更改格式,则更容易将数据存储在dict中。 希望我能帮上忙。
答案 1 :(得分:0)
要获取所需格式的数据,可以将相关部分添加到字典中,然后将其转换为数据框:
import ast
import pandas as pd
with open('text.txt','r', encoding='utf-8') as f:
lines = f.readlines()
d = {"time":[],
"val1":[],
"val2":[]}
for i, line in enumerate(lines):
if line[:5] == "time:":
time = line.strip().split()[-1]
#Reading string representations of lists as lists
v1 = ast.literal_eval(lines[i+2].strip())
v2 = ast.literal_eval(lines[i+4].strip())
#Counting number of vals per date
n1 = len(v1)
n2 = len(v2)
#Padding values if any are missing
if n1 > n2:
v2 += [None] * n1-n2
elif n2 > n1:
v1 += [None] * n2-n1
d["time"].extend([time] * max(n1,n2))
d["val1"].extend(v1)
d["val2"].extend(v2)
df = pd.DataFrame(d)
print(df)
time val1 val2
0 201510010000 1.1 1.00
1 201510010000 1.2 1.01
2 201510010000 1.3 1.02
3 201510010100 2.1 2.01
4 201510010100 2.2 2.02
5 201510010100 2.3 2.03
6 2015020000 3.0 3.01
7 2015020000 3.1 3.02
8 2015020000 3.2 3.03
9 2015020100 4.0 401.00
10 2015020100 4.1 4.02
11 2015020100 4.2 4.03
答案 2 :(得分:0)
我正在学习python,这就是我想出的:) 阅读解决方案并发现错误的人,请指出。
time = ""
val1 = []
val2 = []
final_list = []
process_val1 = False
process_val2 = False
with open('read.txt','r',encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
try:
line = line.strip()
if val1 and val2 and time != '':
for v1, v2 in zip(val1, val2):
final_list.append([time, v1, v2])
val1 = []
val2 = []
time = ''
continue
if process_val1 == True:
val1 = line.split('[')[1].split(']')[0].split(',')
process_val1 = False
continue
if process_val2 == True:
val2 = line.split('[')[1].split(']')[0].split(',')
process_val2 = False
continue
if 'time:' in line:
time = line.split(": ")[1]
continue
elif 'val1' in line:
process_val1 = True
continue
elif 'val2' in line:
process_val2 = True
continue
elif 'va2' in line:
process_val2 = True
continue
else:
continue
except:
#handle exception here
pass
if final_list:
with open('write.txt', 'w') as w:
for list in final_list:
w.write(", ".join(list) + '\n')
答案 3 :(得分:0)
首先,根据您的描述,我假设“经纬度”以下的x1,x2,y1和y2对您没有任何意义。
让我们假设您向我们展示的图片中的数据是您想要的,并且原始数据已格式化为示例(例如,只有两个数据列,即val1和val2; val1和val2始终具有3个值每个时间戳记; val2总是在val1)之后出现,那么以下解决方案应该可以工作:
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
上面的代码所做的是解析给定的文本,并将格式化的输出写入与相同的文件夹中的名为“ output.csv” 的 csv 文件中“ text.txt” 。您可以直接使用MS Excel或任何其他表单表编辑器或查看器打开它。
我在这里使用了regex,因为它最灵活,您可以随时修改模式以适合您的需求,而无需更改其余逻辑。另外,使用标志的优点是不会被文本中可能出现的重复行所混淆。
如果您还有其他要求,请发表评论。