Python:通过分隔符解析复杂文本文件

Python:通过分隔符解析复杂文本文件,python,parsing,text-parsing,string-parsing,praat,Python,Parsing,Text Parsing,String Parsing,Praat,我对Python非常陌生,通常习惯于Java。我目前正在尝试解析Praat输出的文本文件,该文件的格式始终相同,大致如下所示,还有一些其他功能: -- Voice report for 53. Sound T1_1001501_vowels -- Date: Tue Aug 7 12:15:41 2018 Time range of SELECTION From 0 to 0.696562 seconds (duration: 0.696562 seconds) Pitch:

我对Python非常陌生,通常习惯于Java。我目前正在尝试解析Praat输出的文本文件,该文件的格式始终相同,大致如下所示,还有一些其他功能:

-- Voice report for 53. Sound T1_1001501_vowels --
Date: Tue Aug  7 12:15:41 2018

Time range of SELECTION
    From 0 to 0.696562 seconds (duration: 0.696562 seconds)
Pitch:
   Median pitch: 212.598 Hz
   Mean pitch: 211.571 Hz
   Standard deviation: 23.891 Hz
   Minimum pitch: 171.685 Hz
   Maximum pitch: 265.678 Hz
Pulses:
   Number of pulses: 126
   Number of periods: 113
   Mean period: 4.751119E-3 seconds
   Standard deviation of period: 0.539182E-3 seconds
Voicing:
   Fraction of locally unvoiced frames: 5.970%   (12 / 201)
   Number of voice breaks: 1
   Degree of voice breaks: 2.692%   (0.018751 seconds / 0.696562 seconds)
我想输出如下内容:

0.696562,212.598,211.571,23.891,171.685,265.678,126,113,4.751119E-3,0.539182E-3,5.970,1,2.692

所以本质上我想打印出一个字符串,每个行中冒号和后面的空格之间的数字,用逗号分隔。我知道这可能是一个愚蠢的问题,但我就是无法用Python解决它;任何帮助都将不胜感激

好的,这里有一件简单的事情,你需要稍微调整一下才能为你工作

import re
with open("file.txt", "r") as f:
  lines = [s.strip() for s in f.readlines()]
  numbers_list = []
  for _ in lines : 
    numbers_list.append(re.findall(r'\d+', _))
  print(numbers_list)

其中file.txt是您的文件。

好的,这里有一些简单的东西,您需要稍微调整一下才能为您工作

import re
with open("file.txt", "r") as f:
  lines = [s.strip() for s in f.readlines()]
  numbers_list = []
  for _ in lines : 
    numbers_list.append(re.findall(r'\d+', _))
  print(numbers_list)
其中file.txt是您的文件。

可能:

for line in text.splitlines():
         line=line.strip()
         head,sepa,tail=line.partition(":")
         if sepa:
             parts=tail.split(maxsplit=1)
             if parts and all( ch.isdigit() or ch in ".eE%-+" for ch in parts[0]):
                 num=parts[0].replace("%"," ")
                 try:
                     print(float(num.strip()))
                 except ValueError:
                     print("invalid number:",num)
输出:

也许:

输出:


谢谢大家的帮助!我实际上想出了这个解决方案:

import csv

input = 't2_5.txt'
input_name = input[:-4]

def parse(filepath):
data = []
with open(filepath, 'r') as file:
    file.readline()
    file.readline()
    file.readline()
    for line in file:
        if line[0] == ' ':
            start = line.find(':') + 2
            end = line.find(' ', start)
            if line[end - 1] == '%':
                end -= 1
            number = line[start:end]
            data.append(number)
with open(input_name + '_output.csv', 'wb') as csvfile:
    wr = csv.writer(csvfile)
    wr.writerow(data)

parse(input)

谢谢大家的帮助!我实际上想出了这个解决方案:

import csv

input = 't2_5.txt'
input_name = input[:-4]

def parse(filepath):
data = []
with open(filepath, 'r') as file:
    file.readline()
    file.readline()
    file.readline()
    for line in file:
        if line[0] == ' ':
            start = line.find(':') + 2
            end = line.find(' ', start)
            if line[end - 1] == '%':
                end -= 1
            number = line[start:end]
            data.append(number)
with open(input_name + '_output.csv', 'wb') as csvfile:
    wr = csv.writer(csvfile)
    wr.writerow(data)

parse(input)

你能包括到目前为止你尝试过的吗?你能包括到目前为止你尝试过的吗?我认为剥离的问题是,在某些情况下,字符串中有一些我不想包括的数字,例如,我不想让发音下括号之间的数字,但我确实想要“E”在一些数字的科学记数法中。这就是为什么我一直在尝试在“:”和它后面的空白之间指定子字符串的原因。例如,您可以在检查整数的正则表达式之前执行另一个正则表达式,或者使用if语句检查字符串中是否有“:”等。正如我所说,这将得到所有的数字,然后取决于你如何调整它。我认为剥离的问题是,在某些情况下,字符串中有一些数字我不想包括在内,例如,我不希望在发音下括号之间的数字,但我确实想要“E”在一些数字的科学记数法中。这就是为什么我一直在尝试在“:”和它后面的空白之间指定子字符串的原因。例如,您可以在检查整数的正则表达式之前执行另一个正则表达式,或者使用if语句检查字符串中是否有“:”等。正如我所说,这将得到所有的数字,然后取决于你如何调整it@ling-分析对于python新手来说,有两种结构需要学习:1)我在all()中使用的生成器理解;2) try…except条款。(我必须将'except'改为'except ValueError')@ling analysis对于python新手来说,有两种构造需要学习:1)我在all()中使用的生成器理解;2) try…except条款。(我必须将'except'更改为'except ValueError')