0

I need to read these lines from a .txt file:

Turn  60 Player  -1
board: [[ 0  0  0  0  1  2  0  6 12  0  1  0  0  5  5 21]]
action p-values: [0.0, 0.0, 0.0, 0.0, 0.6326530612244898, 0.3673469387755102, 0.0]
nn: legal moves:[4, 5]
nn: select: 4
nn: db_lookup  0 0 0 0 1 2 0 6 12 0 1 0
nn: scores: [127, 127, 127, 127, -4, -5]
nn: best move selected

How can I extract the number in the array action p-values?

I need to create the same array.

This is my starting point:

with open(match, 'r') as searchfile:
        for line in searchfile:
            if 'Turn' in line:
                line = next(searchfile)
                line = next(searchfile)
                if 'p-values' in line:
                    line.rstrip('\n')
                    fields = line.split(": ")
                    pvalues.append(fields[1])

But if i try to print pvalues I get an array with strings inside (included the \n). How can I have in pvalues and array with inside arrays of float?

Thanks

Davide
  • 185
  • 3
  • 22
  • Possible duplicate of [Convert string representation of list to list](https://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list) – m.rp Jun 20 '19 at 15:13

3 Answers3

2

You can use ast library to convert string to list and also there is a bug in your rstrip. See below.

import ast
pvalues=[]
with open('match', 'r') as searchfile:
        for line in searchfile:
            if 'Turn' in line:
                line = next(searchfile)
                line = next(searchfile)
                if 'p-values' in line:
                    line=line.rstrip('\n')
                    fields = line.split(": ")
                    pvalues.append(ast.literal_eval(fields[1]))
pvalues

Result:
[[0.0, 0.0, 0.0, 0.0, 0.6326530612244898, 0.3673469387755102, 0.0]]
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
1

You can first split based on : and then strip the new line character \n followed by using literal_eval to convert the string to list

from ast import literal_eval

with open(match, 'r') as searchfile:
    for line in searchfile:
        if 'Turn' in line:
            line = next(searchfile)
            line = next(searchfile)
            if 'p-values' in line:
                fields = line.split(": ")
                print (fields)
                pvalues = literal_eval(fields[1].strip('\n'))

print (pvalues)
# [0.0, 0.0, 0.0, 0.0, 0.6326530612244898, 0.3673469387755102, 0.0]
Sheldore
  • 37,862
  • 7
  • 57
  • 71
0

Accurately with re.findall function:

import re

with open('file.txt') as f:
    pvalues = []
    pat = re.compile(r'\d+\.?\d+')
    for line in f:
        if 'Turn' in line:
            next(f)
            line = next(f)
            if 'p-values' in line:
                num_str = line.strip().split(": ")[1]
                pvalues.append(list(map(float, pat.findall(num_str))))

print(pvalues)

The output (list of lists with float numbers):

[[0.0, 0.0, 0.0, 0.0, 0.6326530612244898, 0.3673469387755102, 0.0]]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105