#๐ IndexError while reading a file via readlines method
49 messages ยท Page 1 of 1 (latest)
@urban reef
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.
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
i is counting lines, but you're using it to index the line itself
Why iterate the file and also call readline?
because I want the line number
because user data has been saved on the basis of line number
Like dis -
- abc
- def
- ghi
etc.
I don't see why that requires you to call readline.
line is already each line.
what shall I call then? If I have to find the index of a line with a particular word
Like I mentioned, line is already each line. Just use line.
can u tell me how?
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.
category = file_to_load.line()
^^^^^^^^^^^^^^^^^
AttributeError: '_io.TextIOWrapper' object has no attribute 'line'
I am kinda new to python sorry
No. Just line. Replace the whole thing with line.
oh thanks it worked!
You're welcome.
There's another error lol
It is unable to find the category from the category_list
What does it show if you print out the category list first?
Language
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
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.
how?
Strings has a strip method.
oh okay lemme use it
^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: index() takes at least 1 argument (0 given)```
error
What argument does it take
!d str.index
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.
Why are you calling index?
to know the index of the category to access it's value from a different file through readlines[index].
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.
Yep, there you go.
Thank u so much, it worked ๐
You're welcome.
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.