PYTHON:在文本文件中读取不适用于分隔符

时间:2016-02-12 11:23:37

标签: python csv

我有一个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导入仍然适合还是有更强大的功能?

2 个答案:

答案 0 :(得分:3)

使用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']

答案 1 :(得分:2)

csv.reader仍然可以与您的用例相关,请查看csv.reader中skipinitialspace参数的使用

csv.reader(csvfile, delimiter= ' ', skipinitialspace=True)

这将导致文件由空格分隔,但分隔符后面的其他空格将被忽略。

r = csv.reader(csvfile, delimiter= ' ', skipinitialspace=True)
for row in r:
    print row

['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', '...'] `

然后,您只能使用每行的前2个值