Python文本文件解析与逻辑方法

2024-09-26 18:11:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有点迷恋python逻辑。
我想就如何处理python遇到的问题和解析数据的方法提出一些建议。在

我花了一点时间阅读python参考文档并浏览了这个站点,我知道有几种方法可以实现我想要实现的目标,这就是我所走的道路。
我正在用一些卫星硬件生成的数据重新格式化一些文本文件,然后上传到MySQL数据库中。在

这是原始数据

TP N: 1   
Frequency: 12288.635 Mhz   
Symbol rate: 3000 KS  
Polarization: Vertical  
Spectrum: Inverted  
Standard/Modulation: DVB-S2/QPSK  
FEC: 1/2  
RollOff: 0.20  
Pilot: on  
Coding mode: ACM/VCM  
Short frame  
Transport stream
Single input stream  
RF-Level: -49 dBm  
Signal/Noise: 6.3 dB  
Carrier width: 3.600 Mhz  
BitRate: 2.967 Mbit/s  

对卫星上的每个转发器TP N重复上述部分
我用这个脚本提取我需要的数据

^{pr2}$

然后,在将输出数据发送到数据库之前,输出数据的格式如下所示

12288.635 Mhz
 3000 KS
 Vertical
 DVB-S2/QPSK
 1/2
 -49 dBm
 6.3 dB  
 3.600 Mhz
 2.967 Mbit/s

这个脚本运行得很好,但是对于一些我想在MySQL上实现的特性,我需要进一步编辑它。在

  1. 删除小数点和它后面的3个数字以及第一个“频率”行上的MHz。在
  2. 删除所有尾随的度量引用KSdBmdBMhzMbit。在
  3. 将9个字段合并为逗号分隔的字符串,这样每个转发器(每个文件大约30个)都在各自的行上

我不确定是否要继续沿着这条路径添加到现有的脚本中(我在输出文件写入的地方卡住了)。或者重新考虑我处理原始文件的方式。在


Tags: 文件数据方法脚本数据库dbmysqlks
2条回答

我的解决方案是粗糙的,可能不适用于角落案件,但这是一个良好的开端。在

import re
import csv

strings = ("Frequency", "Symbol", "Polar", "Mod", "FEC", "RF", "Signal", "Carrier", "BitRate")  
sat_raw = open('/BLScan/reports/1520.txt', 'r') 
sat_out = open('1520out.txt', 'w')
csv_writer = csv.writer(sat_out)
csv_output = []
for line in sat_raw:
    if any(s in line for s in strings): 
        try:
            m = re.match(r'^.*:\s+(\S+)', line)
            value = m.groups()[0]
            # Attempt to convert to int, thus removing the decimal part
            value = int(float(value))
        except ValueError:
            pass # Ignore conversion
        except AttributeError:
            pass # Ignore case when m is None (no match)
        csv_output.append(value)
    elif line.startswith('TP N'):
        # Before we start a new set of values, write out the old set
        if csv_output:
            csv_writer.writerow(csv_output)
            csv_output=[]

# If we reach the end of the file, don't miss the last set of values
if csv_output:
    csv_writer.writerow(csv_output)

sat_raw.close()
sat_out.close()

讨论

  • csv包有助于csv输出
  • re(正则表达式)模块帮助解析行并从行中提取值。在
  • value = int(...)的行中,我们试图将字符串值转换为整数,从而删除点和后面的数字。在
  • 当代码遇到以“tpn”开头的行时,这表示一组新值。我们将旧的值集写入CSV文件。在
import math

strings = ("Frequency", "Symbol", "Polar", "Mod", "FEC", "RF", "Signal", "Carrier", "BitRate")  

files=['/BLScan/reports/1520.txt']
sat_out = open('1520out.txt', 'w') 
combineOutput=[]
for myfile in files:
    sat_raw = open(myfile, 'r') 
    singleOutput=[]
    for line in sat_raw: 
        if any(s in line for s in strings):
            marker=line.split(':')[1]
            try:
                data=str(int(math.floor(float(marker.split()[0]))))
            except:
                data=marker.split()[0]
            singleOutput.append(data)
    combineOutput.append(",".join(singleOutput))    

for rec in combineOutput:
    sat_out.write("%s\n"%rec)
sat_raw.close()
sat_out.close()

files列表中添加所有要分析的文件。它将把每个文件的输出写成一行,每个字段用逗号隔开。在

相关问题 更多 >

    热门问题