Skip to main content
 首页 » 编程设计

python之从 txt 文件中读取列表中的数字,但最多只能是逗号

2024年12月31日8del

这是来自实验室实验的数据(大约 717 行数据)。我不想试图超越它,而是想在 python 或 matlab 上导入并绘制它。顺便说一下,我是新来的...而且是一名学生!

"" 
"Test Methdo","exp-l Tensile with Extensometer.msm" 
"Sample I.D.","Sample108.mss" 
"Speciment Number","1" 
 
"Load (lbf)","Time (s)","Crosshead (in)","Extensometer (in)" 
 
62.638,0.900,0.000,0.00008 
122.998,1.700,0.001,0.00012 

more numbers : see Screenshot of more data from my file

直到出现逗号,我才明白如何阅读整行。具体来说,我需要我的一个数组/列表的加载编号,例如,在第一行我只需要 62.638(这将是我的列表/数组中第一个索引的第一个数字)。

我怎样才能得到这个数组/列表,迭代/读取列表并忽略字符串的东西?

谢谢!

注意:我使用 Anaconda + Jupyter Notebooks for Python 和 Matlab(学校提供的软件)。

编辑:好的,所以我今天回到家并再次处理它。我以前没有处理过 CSV 文件,但经过一些搜索后,我能够学习如何阅读我的文件。

import csv 
from itertools import islice 
 
with open('Blue_bar_GroupD.txt','r') as BB: 
    BB_csv = csv.reader(BB) 
    x = 0 
    BB_lb = [] 
    while x < 7: #to skip the string data 
        next(BB_csv) 
        x+=1 
    for row in islice(BB_csv,0,758): 
        print(row[0]) #testing if I can read row data 

好的,这就是我被困的地方。我想制作一个每行都有第 0 个索引值的数组/列表。对不起,如果我是个菜鸟!

再次感谢!

请您参考如下方法:

您可以跳过所有行直到第一个数据行,然后将数据解析为列表供以后使用——700 多行可以在内存中轻松处理。

为此你需要:

  • 逐行读取文件
    • 记住数字/逗号/点之前的最后一个非空行(== header)
    • 查看该行是否只有数字/逗号/点,否则增加一个跳过计数器(==数据)
  • 寻找到 0
  • 跳过足够多的行以到达标题或数据
  • 将其余部分读入数据结构

创建测试文件:

text = """ 
"" 
"Test Methdo","exp-l Tensile with Extensometer.msm" 
"Sample I.D.","Sample108.mss" 
"Speciment Number","1" 
 
"Load (lbf)","Time (s)","Crosshead (in)","Extensometer (in)" 
 
62.638,0.900,0.000,0.00008 
122.998,1.700,0.001,0.00012 
"""  
with open ("t.txt","w") as w: 
    w.write(text) 

一些助手和跳过/阅读逻辑:

import re 
import csv 
 
def convert_row(row): 
    """Convert one row of data into a list of mixed ints and others. 
    Int is the preferred data type, else string is used - no other tried.""" 
    d = [] 
    for v in row: 
        try: 
            # convert to int && add 
            d.append(float(v)) 
        except:  
            # not an int, append as is 
            d.append(v) 
    return d 
 
def count_to_first_data(fh): 
    """Count lines in fh not consisting of numbers, dots and commas. 
    Sideeffect: will reset position in fh to 0.""" 
    skiplines = 0 
    header_line = 0 
    fh.seek(0) 
    for line in fh: 
        if re.match(r"^[\d.,]+$",line): 
            fh.seek(0) 
            return skiplines, header_line 
        else: 
            if line.strip(): 
                header_line = skiplines 
            skiplines += 1 
    raise ValueError("File does not contain pure number rows!") 

助手的使用/数据转换:

data = [] 
skiplines = 0 
with open("t.txt","r") as csvfile: 
    skip_to_data, skip_to_header = count_to_first_data(csvfile) 
 
    for _ in range(skip_to_header): # skip_to_data if you do not want the headers 
        next(csvfile) 
    reader = csv.reader(csvfile, delimiter=',',quotechar='"') 
    for row in reader: 
        row_data = convert_row(row)  
        if row_data: 
            data.append(row_data) 
 
print(data) 

输出(重新格式化):

[['Load (lbf)', 'Time (s)', 'Crosshead (in)', 'Extensometer (in)'],  
 [62.638, 0.9, 0.0, 8e-05],  
 [122.998, 1.7, 0.001, 0.00012]] 

独库:


有了这个,您现在拥有可用于进一步处理的“干净”数据 - 包括您的 header 。

对于可视化,您可以查看 matplotlib