-1

file = open(fname, 'r') codes = re.findall('LIR: (.+)', file.read()) functions = re.findall(';(.+);LIR', file.read())

Im trying to extract 2 different strings from every 1 line in single file.

It gets SyntaxError: unexpected EOF while parsing

Nikko
  • 1,410
  • 1
  • 22
  • 49
  • 2
    @WiktorStribiżew: its not actually closed as such, it has read to EOF in the first `read()`. – cdarke Jun 01 '18 at 07:37
  • @cdarke Yeah, it is at the EOF, and it cannot be read further. – Wiktor Stribiżew Jun 01 '18 at 07:38
  • I am not a python expert but: `file = open(fname, 'r') codes = re.findall('LIR: (.+)', file.read()) file = open(fname, 'r') functions = re.findall(';(.+);LIR', file.read())` should do the trick right? – Allan Jun 01 '18 at 07:38
  • 1
    @WiktorStribiżew - I'm being pedantic, but actually it could be read if there was a `seek()` backwards. But that's not what the OP wants anyway. – cdarke Jun 01 '18 at 07:43
  • @cdarke Yeah, by "further" I mean "further forward" :) – Wiktor Stribiżew Jun 01 '18 at 07:44

1 Answers1

0

The first time your code calls file.read() it is reading the whole file. The second time your code calls it, without first closing the file and reopening it, it is at EOF.

If you want to extract stuff from individual lines of the file then iterate through the file:

codes = []
functions = []
with open(fname,'r') as file:
    for line in file:
        codes.extend(re.findall('LIR: (.+)',line))
        functions.extend(re.findall(';(.+);LIR', line))
BoarGules
  • 16,440
  • 2
  • 27
  • 44