Reputation: 12061
I have a very large file and I want to split each row twice and return it. However I am only able to get the first result and the second is out of bounds
def clean_text(doc):
rows = doc.split('\n')
for row in rows:
row = row.split('|')
first_name = row[0]
last_name = row[1] <---- out of bounds
print(first_name, last_name)
Is there a nicer solution?
Upvotes: 0
Views: 458
Reputation: 668
After you do this check your resulting files and see all the ones without last name what is so special about them. From that point you can decide how to handle those cases.
If your input data(doc) is not something in your control than just reject if it is wrong or handle it by catching exception. It is pointless for you to ask for best approach without specifying requirements and preconditions for your method.
def clean_text(doc):
rows = doc.splitlines()
for row in rows:
row = row.split('|')
first_name = row[0]
try:
last_name = row[1]
except IndexError:
last_name = ''
print(first_name, last_name)
Upvotes: 1