I am working on a script that parses a text file in an attempt to normalize it enough to be able to insert it in to a DB. The data represents articles written by 1 or more authors. The problem I am having is that because there is not a fixed number of authors, I get a variable number of columns in my output text file. eg.
author1, author2, author3, this is the title of the article
author1, author2, this is the title of the article
author1, author2, author3, author4, this is the title of the article
These results give me a max column number of 5. So, for the first 2 articles I will need to add blank columns so that the output has an even number of columns. What would be the best way to do this? My input text is tab delimited and I can iterate through them fairly easily by splitting on the tab.
Assuming you already have the max number of columns and already have them separated into lists (which I’m going to assume you put into a list of their own), you should be able to just use list.insert(-1,item) to add empty columns:
Alternative version that doesn’t destroy your original list, using list comprehensions: