0

Python version 2.7.13, main.py

import csv
with open(r"int.txt") as f:
   reader = csv.reader(f, delimiter="\t")
   d = list(reader)
print d[1][1]

int.txt file is as below

1   577 7
7   85  888
0   86  876

I want print d[1][1] to return 85, but it throws the following error.

sh-4.4$ python main.py                                                                                                                                                                                        
Traceback (most recent call last):                                                                                                                                                                            
  File "main.py", line 15, in <module>                                                                                                                                                                        
    print d[1][1]                                                                                                                                                                                             
IndexError: list index out of range 

Any thoughts would be appreciated. I made sure to check that the text file has no blank lines, referencing similar questions.

2
  • 3
    That doesn't look tab-delimited to me. Commented Jul 3, 2018 at 20:24
  • 1
    I put tabs in the obvious places, and got a very nice `'85'1 as the result. I don't know that you have tabs in the supplied file. Commented Jul 3, 2018 at 20:25

1 Answer 1

1

Your text file is not tab-delimited. Either change your delimiter to specific white space or change your .txt file to have a tab between each number.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I didn't realize that using the Tab key, on the keyboard won't necessarily delimit my text file. Now, I've put it into excel and copied into text, which gives me the proper output.
If this helped you, how about selecting it as the answer that helped you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.