简体   繁体   中英

Python3.7 not parsing text file properly

I am attempting to write a (should be...) basic Python script to accomplish the following:

  1. Read in a log file from a hardcoded path (example file below)
  2. Create an array of each line of the file, with two or three elements
  3. Print out that array.

Here is an example log file from the scimark benchmark test:

**                                                              **
** SciMark2 Numeric Benchmark, see http://math.nist.gov/scimark **
** for details. (Results can be submitted to pozo@nist.gov)     **
**                                                              **
Using       2.00 seconds min time per kenel.
Composite Score:          55.11
FFT             Mflops:   35.99    (N=1024)
SOR             Mflops:   60.25    (100 x 100)
MonteCarlo:     Mflops:    3.21
Sparse matmult  Mflops:   16.10    (N=1000, nz=5000)
LU              Mflops:   15.02    (M=100, N=100)

Ideally, I would be creating an array like so:

array = [
['Composite Score', 55.11 ''],
['FFT MFlops', 35.99, '(N=1024)'],
['SOR MFlops', 60.25, '(100 x 100)'],
['MonteCarlo Mflops', 3.21, ''],
['Sparse matmult Mflops', 16.10, '(n=1000, NZ=5000)'],
['LU', 3.21, '(M=100, N=100)']]

I have tried to do this with the following python codes:

import csv

with open ('/SciMarkResults.txt') as file:
    lines = file.readlines()

print(len(lines))
new_lines = lines[5:]

def get_data(readfile):
    types = (line.split('\n') for line in readfile)
    return types

a = get_data(new_lines)

print(a)

Which provides the following output:

11
<generator object get_data.<locals>.<genexpr> at 0x7ff45b5c5ba0>

I have considered using regular expression, but that seems to be not a preferred solution.

I have not been able to determine why I am not able to split the array properly. Simply printing new_lines yields:

['Composite Score:          460.11\n', 'FFT             Mflops:   315.99    (N=1024)\n', 'SOR             Mflops:   860.25    (100 x 100)\n', 'MonteCarlo:     Mflops:    93.21\n', 'Sparse matmult  Mflops:   416.10    (N=1000, nz=5000)\n', 'LU              Mflops:   615.02    (M=100, N=100)\n']

Any advice would be appreciated.

Instead of using,

types = (line.split('\n') for line in readfile)

which is a generator comprehension, you could use

types = [line.split('\n') for line in readfile]

which is a list comprehension and should give you the output that you need.

The same has been answered above by @jdehesa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM