Hi All,
Just a quick question. I have some text files that are space-delimited. Some of the columns in this file have been padded with zeros so that when you open the text file in a simple text editor, the columns all line up and it is easy to read. I am using csv,DictReader to read in the files, so that it automatically generates a dictionary based off the first row. The problem is that when I read this file in with csv.DictReader, I end up with a lot of blank columns.
here is my read-in line:
isc = csv.DictReader( open(inisc), delimiter=' ')
Is there a way to specify that if python encounters one or more spaces in a row, that they should be treated as 1 delimiter rather than multiple ones?
I tried:
isc = csv.DictReader( open(inisc), delimiter=' '+)
and some similar variations (though I do not remember them all now) to no avail.
If I alter the input text files to remove the padding and have only one space between each column, this works great. I can stick with this method, it is just that for readability of the text files (which can be quite big), the padding is nice. These files are used for other purposes than just input to my code, so if there is a way to keep the padding and make csv.DictReader happy, that would be best.
Thanks,
Monica
Just a quick question. I have some text files that are space-delimited. Some of the columns in this file have been padded with zeros so that when you open the text file in a simple text editor, the columns all line up and it is easy to read. I am using csv,DictReader to read in the files, so that it automatically generates a dictionary based off the first row. The problem is that when I read this file in with csv.DictReader, I end up with a lot of blank columns.
here is my read-in line:
isc = csv.DictReader( open(inisc), delimiter=' ')
Is there a way to specify that if python encounters one or more spaces in a row, that they should be treated as 1 delimiter rather than multiple ones?
I tried:
isc = csv.DictReader( open(inisc), delimiter=' '+)
and some similar variations (though I do not remember them all now) to no avail.
If I alter the input text files to remove the padding and have only one space between each column, this works great. I can stick with this method, it is just that for readability of the text files (which can be quite big), the padding is nice. These files are used for other purposes than just input to my code, so if there is a way to keep the padding and make csv.DictReader happy, that would be best.
Thanks,
Monica
Comment