Pavlos Pavlos - 2 months ago 10
Python Question

Replacing missing values of an input Python

Suppose you have an input formatted like this:


the 4
are supposed to be

So far, I get something like this: the first item has an
{ID:1, value1:hello, value2:world,...,valueN:something}
whereas the second item has
{ID:2, value1: , value2:goodnight, ... , valueN: world}

I want my final representation for the 2nd item to be:
{ID:2, value1:n/a , value2:goodnight, ... , valueN: world}

I have written a script in Python to read the file line by line, but I want to be able to check whether a
is followed by another
, and then insert the

My code so far is this:

def myFunc():
list = []
with open(file, 'r') as f:
header = f.readline() # Store the header of the file for future reference.(maybe). Don't commend out.
for line in f:
for i in range(len(line)):
if line[i] == '\t':
if line[i+1] == '\t':
line[:i] + "n/a" + line[i:]
list.append(line) # iterate through the file and store it's values on the list.
return list


Depending a bit on how you want to use the list at the end of the day, you could also use the csv module for something which will be a bit more flexible for cases where more than one column might come without entries;

import csv

with open(file, 'r') as f:
    reader = csv.reader(f, delimiter='\t')
    header = next(reader)
    list = [[x if x else 'n/a' for x in line] for line in reader]

Now list will be a list of lists, each of which contains the actual items.

In [11]: print(header)
['id', 'value1', 'value2', 'value3']

In [12]: print(list)
[['1', 'hello', 'world', 'something'], ['2', 'n/a', 'goodnight', 'world']]