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?
row[1]then it means thatrow.split('|')returned a list of length=1 which means there was no pipe|in the line.first_name, last_name = row.split('|'), but that won't fix the out of bounds error. Perhaps add in a check before the split:if '|' in row:. Also you can dodoc.splitlines()instead of splitting on\n. See this post and this post.