python - Delete tab character from config files -


i using function read config file.

import numpy np  stream = np.genfromtxt(filepath, delimiter = '\n', comments='#', dtype= 'str') 

it works pretty have problem: tab character.

i.e. output

['\tvalue1 ', ' 1'] ['\t'] ['value2 ', ' 2']

is there way ignore special char?

my solution that: (it works purposes it's bit "ugly")

result = {} el in stream:     row = el.split('=',1)     try:         if len(row) == 2:             row[0] = row[0].replace(' ','').replace('\t','') #clean elements not needed spaces             row[1] = row[1].replace(' ','').replace('\t','')             result[row[0]] = eval(row[1])     except:         print >> sys.stderr,"fatal error: '"+filepath+"' missetted"          logging.exception(sys.stderr)         sys.exit('') 

to replace tabs nothing:

stream = [x.replace('\t','') x in stream] 

or replace tabs single space, , remove duplicate spaces:

stream = [' '.join(x.replace('\t',' ').split()) x in stream] 

to remove empty strings (source):

stream = filter(none, stream) 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -