0

I am using this function to read a config file.

import numpy as np

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

It works pretty well but I have a problem: the tab character.

I.e. output

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

Is there a way to ignore this special char?

My solution is something like that: (It works for my purposes but it's a bit "ugly")

result = {}
for el in stream:
    row = el.split('=',1)
    try:
        if len(row) == 2:
            row[0] = row[0].replace(' ','').replace('\t','') #clean the elements from 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('')
overcomer
  • 2,244
  • 3
  • 26
  • 39
  • Are there any comments in the file? Does the tab character ever occur somewhere within a relevant line? If both no, then you could do `comments='\t'`. – L3viathan Apr 22 '15 at 18:43
  • Yes, there are a lot of comments. I need the option comments='#' – overcomer Apr 22 '15 at 18:45

2 Answers2

1

To replace the tabs with nothing:

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

Or to replace tabs with a single space, and then remove duplicate spaces:

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

To remove empty strings (source):

stream = filter(None, stream)
Community
  • 1
  • 1
philshem
  • 24,761
  • 8
  • 61
  • 127
  • Sure, but this not solve the problem. I still will have empty elements of the array. In the example, the second element. Stream is passed to others function, the empty elements cause errors. – overcomer Apr 22 '15 at 23:04
  • I added another line to remove empty strings – philshem Apr 23 '15 at 06:42
0

There docent seem to be a way to assign multiple delimiters or comments using numpys genfromtext. I would recommend looking elsewhere. Try https://docs.python.org/2/library/configparser.html. Here's a link with a quick example so you can get a feel for how to work with the module https://wiki.python.org/moin/ConfigParserExamples

dparadis28
  • 101
  • 2
  • I used configparser for reading other files in a more "standard" format. I decided to use the python genfromtext because in someway is more flexible. I do not have sections too. Thanks for the clue anyway. – overcomer Apr 22 '15 at 19:32