python中的优雅解决方案以提取数据并将其放置在基本阵列格式下

发布于 2025-02-06 15:39:29 字数 1448 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2025-02-13 15:39:29

很难说以下内容是优雅的还是漂亮的,但我认为这有点“ Pythonic”。我们可以使用以下函数来解析Wolfram输出,该函数将其作为输入输入到该文件的打开文件指针:

def parse_wolfram(file_pointer):
    # the first line is the header, which we ignore
    _ = file_pointer.readline()
    row_str = str()
    out_data = []
    while True:
        # Read each line till EOF stripping leading and trailing white spaces
        line = file_pointer.readline().strip()
        if not line:
            break

        # Append each line as a string to the current row
        row_str += line
        # Find '}' to detect the end of a row
        if line.find('}') > 0:
            # Parse the row:
            # 1. Use the regular expression module to split the string
            #    where the delimiter is one or more of the character set.
            #    This produces a list of string tokens.
            # 2. [1:-1] removes the empty string tokens at the head and 
            #    tail of this list
            # 3. Use list comprehension to cast string tokens to float.
            # 4. Append list of floats for each row to output list of lists (2-D array)  
            out_data.append([float(data) for data in re.split(r'[{, }]+', row_str)[1:-1]])
            # Reset for next row
            row_str = str()

    return out_data

如果该文件格式化为OP建议:该函数可以在名为'chain..m'的文件上使用。

    with open('chain.m', 'r', encoding='utf-8') as fp:
        parsed_output = parse_wolfram(fp)
        
    print(parsed_output)
    [[0.29344728841663786, 0.00037262711145454893, 0.7061800844719075, 67.41431300170986, 1.3887122472912174, 0.0014182932914303275, 500.97644711373647, 0.0002565333937360516, 105.86185844804378], [0.29479428399557506, 0.0007813301223490133, 0.7044243858820759, 67.40475060370453, 1.3779372193629575, 6.103376259459755e-05, 500.30876628350757, 1.106337484454747e-05, 101.39952463245301]]

该输出是浮子列表的python列表。可以使用numpy.Array(parsed_output)将其转换为numpy数组。

Hard to say if the following is elegant or pretty, but I believe that it is somewhat 'pythonic'. We can parse the Wolfram output as specified using the following function that takes as input an opened file pointer to the file:

def parse_wolfram(file_pointer):
    # the first line is the header, which we ignore
    _ = file_pointer.readline()
    row_str = str()
    out_data = []
    while True:
        # Read each line till EOF stripping leading and trailing white spaces
        line = file_pointer.readline().strip()
        if not line:
            break

        # Append each line as a string to the current row
        row_str += line
        # Find '}' to detect the end of a row
        if line.find('}') > 0:
            # Parse the row:
            # 1. Use the regular expression module to split the string
            #    where the delimiter is one or more of the character set.
            #    This produces a list of string tokens.
            # 2. [1:-1] removes the empty string tokens at the head and 
            #    tail of this list
            # 3. Use list comprehension to cast string tokens to float.
            # 4. Append list of floats for each row to output list of lists (2-D array)  
            out_data.append([float(data) for data in re.split(r'[{, }]+', row_str)[1:-1]])
            # Reset for next row
            row_str = str()

    return out_data

This function can be used as such on the file named 'chain.m' if that file is formatted as the OP suggests:

    with open('chain.m', 'r', encoding='utf-8') as fp:
        parsed_output = parse_wolfram(fp)
        
    print(parsed_output)
    [[0.29344728841663786, 0.00037262711145454893, 0.7061800844719075, 67.41431300170986, 1.3887122472912174, 0.0014182932914303275, 500.97644711373647, 0.0002565333937360516, 105.86185844804378], [0.29479428399557506, 0.0007813301223490133, 0.7044243858820759, 67.40475060370453, 1.3779372193629575, 6.103376259459755e-05, 500.30876628350757, 1.106337484454747e-05, 101.39952463245301]]

This output is a python list of lists of floats. This can be converted to a numpy array using numpy.array(parsed_output).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文