Question

I'm reading from CAT pipe in Linux, using subprocess:

stdout=subprocess.PIPE

so some line has BAD EOL, it's huge file and I want to skip such lines and go for the next one. how I can do this in Python?

PS: I always get:

SyntaxError: EOL while scanning string literal

and seems some socket stopped while writing to that file,because I see really huge spaces in the end of that file. Don't want to fix it, want to skip it

here is my code :

import sys,os
import subprocess
import traceback
import re
import ast




try :
        cat = subprocess.Popen(["hadoop", "dfs", "-cat", "PATH TO FILE"], stdout=subprocess.PIPE)
        for data in cat.stdout:
                data = re.sub(' +',' ',data)
                msg= ast.literal_eval(data)
                if  msg['some_string'] == 'some_string' :
                        print msg['status']
                else :
                        continue
except :
        print traceback.format_exc()
        pass
exit()

so the output before the programs exits : many empty spaces and ...

                                                        ^

SyntaxError: EOL while scanning string literal

Was it helpful?

Solution

Here, try this:

import sys,os
import subprocess
import traceback
import re
import ast




try :
        cat = subprocess.Popen(["hadoop", "dfs", "-cat", "PATH TO FILE"], stdout=subprocess.PIPE)
        for data in cat.stdout:
                data = re.sub(' +',' ',data)
                try:
                    msg= ast.literal_eval(data)
                    if  msg['some_string'] == 'some_string' :
                        print msg['status']
                    else :
                        continue
                except SyntaxError:
                    continue #skip this line

except :
        print traceback.format_exc()
        pass
exit()

Hope it helps!

OTHER TIPS

to skip errors you can just code smth like:

try:
    your code
except {Your error}:
    pass

or

try:
    your code
except:
    pass

for all errors

you could also use smth like:

import sys
import traceback

try:
    {code}
except Exception:
    _type, _value, _trace = sys.exc_info()
    print "Type:\n\t{0}\nException:\n\t\t{1}\nTraceback:\n\t{2}".format(
          _type, _value, traceback.format_tb(_trace))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top