我有一个来自实验室设备的txt文件,该文件以以下格式保存数据:

Run1 
Selected data
        Time (s)    Charge Q (nC)   Charge density q (nC/g) Mass (g)    
Initial -   21.53   -2.81E-01   -1.41E-03   200.0   
Flow    -   0.00    0.00E+00    0.00E+00    0.0 
Charge (in Coulomb) temporal evolution
3.61    2.44e-11
4.11    2.44e-11
4.61    2.44e-11
5.11    3.66e-11
5.63    3.66e-11
6.14    2.44e-11
6.66    3.66e-11
7.14    3.66e-11
7.67    2.44e-11
8.19    3.66e-11
8.70    2.44e-11
9.20    2.44e-11
9.72    2.44e-11
10.23   2.44e-11
10.73   2.44e-11

Run2 
Selected data
        Time (s)    Charge Q (nC)   Charge density q (nC/g) Mass (g)    
Initial -   21.53   -2.81E-01   -1.41E-03   200.0   
Flow    -   0.00    0.00E+00    0.00E+00    0.0 
Charge (in Coulomb) temporal evolution
3.61    2.44e-11
4.11    2.44e-11
4.61    2.44e-11
5.11    3.66e-11
5.63    3.66e-11
6.14    2.44e-11
6.66    3.66e-11
7.14    3.66e-11
7.67    2.44e-11
8.19    3.66e-11

Run3 
Selected data
        Time (s)    Charge Q (nC)   Charge density q (nC/g) Mass (g)    
Initial -   21.53   -2.81E-01   -1.41E-03   200.0   
Flow    -   0.00    0.00E+00    0.00E+00    0.0 
Charge (in Coulomb) temporal evolution
3.61    2.44e-11
4.11    2.44e-11
4.61    2.44e-11
5.11    3.66e-11
5.63    3.66e-11
6.14    2.44e-11
6.66    3.66e-11
7.14    3.66e-11
7.67    2.44e-11
8.19    3.66e-11
8.70    2.44e-11
9.20    2.44e-11

我的测试文件夹中有多个.我希望简化和自动化对这些数据集的分析,因为对于另一台设备,我用更简单的代码取得了类似的成功.

我要做的是从每个文件名为的文件中提取3次运行中每个运行的2列测试数据,并导出到一个以逗号分隔的文本文件中,文件名=FileName Run#.txt文件

到目前为止,我所做的是try 将文本文件内容转换为列表列表,然后try 将数字数据单独处理为新的csv,但由于我无法检测到我感兴趣的列数据的长度,因此效果不佳.

这里有几个其他Q-A在这方面提供了帮助,包括如何在文件夹中的文件上运行代码,如果可以的话.

我用了一个jupyter笔记本——如果代码有用的话,我可以在这里分享我写的代码,尽管我羞于展示它.

推荐答案

try 以下操作:

import re
from pathlib import Path

input_path = Path("path/to/input_folder")
output_path = Path("path/to/output_folder")
run_name_pattern = re.compile("Run\d+")
data_line_pattern = re.compile("(.+?) +(.+?)")


def write_output(input_file: Path, run_name: str, data: str):
    output_file = output_path / f"{input_file.stem}-{run_name}.csv"
    with output_file.open("w") as fp_out:
        fp_out.write(data)


for input_file in input_path.glob("*.txt"):
    with input_file.open() as fp:
        run_name, data, start_reading = "", "", False

        for line in fp:
            # If a line matches "Run...", start a new run name
            if run_name_pattern.match(line):
                run_name = line.strip()
            # If the line matches "Charge (in Coulomb)...",
            # read in the data, starting with the next line
            elif line.startswith("Charge (in Coulomb) temporal evolution"):
                start_reading = True
            # For the data lines, replace spaces in the middle with a comma
            elif start_reading and line != "\n":
                data += data_line_pattern.sub(r"\1,\2", line)
            # If we encounter a blank line, that means the end of data.
            # Flush the data to disk.
            elif line == "\n":
                write_output(input_file, run_name, data)
                run_name, data, start_reading = "", "", False
        else:
            # If we have reached the end of the file but there still
            # data we haven't written to disk, flush it
            if data:
                write_output(input_file, run_name, data)

Python相关问答推荐

实现的差异取决于计算出的表达是直接返回还是首先存储在变量中然后返回

跟踪我已从数组中 Select 的样本的最有效方法

Python中使用时区感知日期时间对象进行时间算术的Incredit

Python 3.12中的通用[T]类方法隐式类型检索

什么相当于pytorch中的numpy累积ufunc

加速Python循环

基于字符串匹配条件合并两个帧

使用密钥字典重新配置嵌套字典密钥名

Scrapy和Great Expectations(great_expectations)—不合作

如何启动下载并在不击中磁盘的情况下呈现响应?

如何使regex代码只适用于空的目标单元格

为什么if2/if3会提供两种不同的输出?

如何在两列上groupBy,并使用pyspark计算每个分组列的平均总价值

使用Openpyxl从Excel中的折线图更改图表样式

Python类型提示:对于一个可以迭代的变量,我应该使用什么?

需要帮助使用Python中的Google的People API更新联系人的多个字段'

Pandas数据框上的滚动平均值,其中平均值的中心基于另一数据框的时间

为什么在Python中00是一个有效的整数?

随机森林n_估计器的计算

如何在Quarto中的标题页之前创建序言页