使用 python 读取 .txt 数据

Read .txt data using python

我有一个这样的 .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,)

这就是我所做的,但我不知道下一步。

我怎样才能到达它?

我建议您更改.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])

该代码可能适用于您的文件,但如果您更改格式,将更容易将数据存储在字典中。 希望对您有所帮助。

要获取所需格式的数据,您可以将相关部分添加到字典中,然后将其转换为数据帧:

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

我正在学习 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')

首先,根据你的描述,我认为“经纬度”下面的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 或任何其他电子表格编辑器或查看器打开它。

我在这里使用了正则表达式,因为它最灵活,您可以随时修改模式以满足您的需要,而无需更改其余逻辑。此外,使用标志的优点是不会被文本中可能出现的重复行混淆。

如有其他需求,欢迎留言。