gpt4 book ai didi

PYTHON:读取文本文件不适用于定界符

转载 作者:行者123 更新时间:2023-11-28 21:48:26 25 4
gpt4 key购买 nike

我有一个从 gem5 输出的文本文件(即,我无法控制它的格式)。

是这样的:

    ---------- Begin Simulation Statistics ----------
sim_seconds 9.553482 # Number of seconds simulated
sim_ticks 9553481748000 # Number of ticks simulated
final_tick 9553481748000 # Number of ticks from beginning of simulation (restored from checkpoints and never reset)
sim_freq 1000000000000 # Frequency of simulated ticks
host_inst_rate 911680 # Simulator instruction rate (inst/s)
host_op_rate 1823361 # Simulator op (including micro ops) rate (op/s)
host_tick_rate 1669871119 # Simulator tick rate (ticks/s)
host_mem_usage 662856 # Number of bytes of host memory used
host_seconds 5721.09 # Real time elapsed on the host
sim_insts 5215804132 # Number of instructions simulated
sim_ops 10431608523 # Number of ops (including micro ops) simulated

使用 csv 模块时,我遇到了空格分隔行的问题。如果我用空格分隔,所有空格都会被读入,如果我用\t 分隔,它根本不会确认任何内容。

我怎样才能轻松处理这些空格,因为我只想在左栏中阅读以及赋予它的值。

csv import 是否仍然适用,还是有更强大的东西?

最佳答案

使用 re.split 拆分:

import re

d = """ ---------- Begin Simulation Statistics ----------
sim_seconds 9.553482 # Number of seconds simulated
sim_ticks 9553481748000 # Number of ticks simulated
final_tick 9553481748000 # Number of ticks from beginning of simulation (restored from checkpoints and never reset)
sim_freq 1000000000000 # Frequency of simulated ticks
host_inst_rate 911680 # Simulator instruction rate (inst/s)
host_op_rate 1823361 # Simulator op (including micro ops) rate (op/s)
host_tick_rate 1669871119 # Simulator tick rate (ticks/s)
host_mem_usage 662856 # Number of bytes of host memory used
host_seconds 5721.09 # Real time elapsed on the host
sim_insts 5215804132 # Number of instructions simulated
sim_ops 10431608523 # Number of ops (including micro ops) simulated"""

# Skip first line
for line in d.split("\n")[1:]:
# Columns are separated by runs of spaces. Only get three parts.
parts = re.split(r'\s+', line, 3)
# Only print the first two columns.
print(parts[:2])

输出:

['sim_seconds', '9.553482']
['sim_ticks', '9553481748000']
['final_tick', '9553481748000']
['sim_freq', '1000000000000']
['host_inst_rate', '911680']
['host_op_rate', '1823361']
['host_tick_rate', '1669871119']
['host_mem_usage', '662856']
['host_seconds', '5721.09']
['sim_insts', '5215804132']
['sim_ops', '10431608523']

关于PYTHON:读取文本文件不适用于定界符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com