0

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?

5
  • 3
    If you're getting an out of bounds error with row[1] then it means that row.split('|') returned a list of length=1 which means there was no pipe | in the line. Commented Jan 8, 2021 at 15:06
  • 1
    You can do 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 do doc.splitlines() instead of splitting on \n. See this post and this post. Commented Jan 8, 2021 at 15:06
  • You could also catch the exception, or check the length returned.. Commented Jan 8, 2021 at 15:08
  • You could also try ti solve the problem at the source, and make sure it always creates files with first and last names. Commented Jan 8, 2021 at 15:09
  • You need to add a check on the number of entries returned from the split. Often there are special cases in text files that don't quite conform to the expected pattern. Commented Jan 8, 2021 at 15:11

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.