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
Post a Comment