#๐Ÿ”’ IndexError while reading a file via readlines method

49 messages ยท Page 1 of 1 (latest)

urban reef
oblique raptorBOT
#

@urban reef

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

urban reef
#

elif(UserPrompt.lower() == "load"):
with open("User_Data_Categories.md", "r") as file_to_load:
i = 0
for line in file_to_load:
category = file_to_load.readline()[i]
print(category)
i += 1
categories_list.append(category)
code sample

#

category = file_to_load.readline()[i]
~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: string index out of range

ocean dove
#

i is counting lines, but you're using it to index the line itself

#

Why iterate the file and also call readline?

urban reef
#

because user data has been saved on the basis of line number

#

Like dis -

  1. abc
  2. def
#
  1. ghi
#

etc.

ocean dove
#

line is already each line.

urban reef
ocean dove
urban reef
ocean dove
#

Just replace the call to readline with line.

#

And don't index the line with i, since that's the actual cause of the error.

urban reef
#

I am kinda new to python sorry

ocean dove
#

No. Just line. Replace the whole thing with line.

urban reef
ocean dove
#

You're welcome.

urban reef
#

It is unable to find the category from the category_list

ocean dove
#

What does it show if you print out the category list first?

urban reef
#
            if category_To_Load in categories_list:
                Value_to_load = category_To_Load.index()
                with open("User_Data_Value.md", "r") as file_value_to_load:
                    Value_to_load_Final =  file_value_to_load.readline[Value_to_load]
                    print(f"{category_To_Load}: {Value_to_load_Final}")   ```
#

The error might be here

ocean dove
#

I think it keeps the newlines at the end of the lines, which won't be present when taking input via input. You can stop the string to remove the new lines.

ocean dove
#

Strings has a strip method.

urban reef
#
                    ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: index() takes at least 1 argument (0 given)```
#

error

#

What argument does it take

ocean dove
#

!d str.index

oblique raptorBOT
#

str.index(sub[, start[, end]])```
Like [`find()`](https://docs.python.org/3/library/stdtypes.html#str.find), but raise [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError) when the substring is not found.
ocean dove
#

Why are you calling index?

urban reef
#

Oh now I know what's the mistake

#

Value_to_load = categories_list.index(category_To_Load)

#

This is how it had to be written ig.

ocean dove
urban reef
ocean dove
oblique raptorBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.