PYTHON:在文本文件中读取不会';不能使用分隔符
我有一个来自gem5的文本文件输出(也就是说,我无法控制它的格式) 因此:PYTHON:在文本文件中读取不会';不能使用分隔符,python,csv,Python,Csv,我有一个来自gem5的文本文件输出(也就是说,我无法控制它的格式) 因此: ---------- Begin Simulation Statistics ---------- sim_seconds 9.553482 # Number of seconds simulated sim_ticks 95534817480
---------- 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导入仍然合适还是有更强大的功能?使用以下方式拆分:
输出:
['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']
csv.reader仍然与您的用例相关,请查看csv.reader中
skipinitialspace
参数的用法
csv.reader(csvfile,分隔符=“”,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个值
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', '...'] `