Why might Python break down halfway through a loop? TypeError: __getitem__

2024/10/10 8:23:21

The Goal

I have a directory with 65 .txt files, which I am parsing, one by one, and saving the outputs into 65 corresponding .txt files. I then plan to concatenate them, but I'm not sure if jumping straight to that might help find a solution here.

The Problem

I am receiving:

TypeError: 'NoneType' object has no attribute 'getitem'

and have seen two similar threads:

TypeError: 'NoneType' object has no attribute '__getitem__'

Python: TypeError: 'NoneType' object has no attribute '__getitem__'

My problem seems somewhat strange, however, as it does manage to go through the input files, parsing them and writing the output file about ten times, at which point I get the error. The files are all similar, just HTML source code from website (i.e. the same website, just different pages of it, and so the same basic HTML structure).

Here is the function where the error occurs; in the last line of this snippet:

def parse(elTree):desired_value = elTree.xpath('my_very_long_xpath')desired_value = [x.get('title')[8:] for x in desired_value]

I do have a few more variants of these - I am actually parsing for about 5 to 6 different desired_values. And all of this is simply running inside of a larger loop where the files are read in to the parse function and then the output is written to a new file.

What I have tried

I have removed the file where I initially got the error, but the same error occurred at the next file. I did the same again, removing two files, but still getting that error.

I introduced a time.sleep(3) between each file, just to allow things to maybe run more smoothly. I realized there may be a buffer for the whole process, which is maybe being read and it is just being wiped, and so there is no file there... Here is a similar occurrence within a loop in C. Unfortunately the sleep for 3 seconds (plus then scattered around at various other points) didn't help me. the code fails at exactly the same point.

According to the documentation, a TypeError arises when a function is applied to an object of inappropriate type, so how can it be that it is occurring after functioning correctly 10 or 11 times? Here is more official information regarding the __getitem__ method

As the code does work well otherwise, I haven't included the rest, but if someone suspects it may originate from somewhere else, with good reason, then I will add more of the code.

I have inspected the contents of the .txt files for those that worked and those where it failed and the xpaths work in both, the contents are there to be found and parsed.

I used the code on 30 copies of the same file, which did execute successfully, so there must be subtle differences in the HTML code, which my parser is not recognizing.

Answer

TypeError: 'NoneType' object has no attribute '__getitem__' means that you attempted to use some kind of indexing, like mylist[2], on None instead of on something like a list. This means that the internal call to that object's __getitem__ failed, because None, which is an object of type Nonetype, doesn't have such a method defined for it.

The problem is in x.get('title')[8:]: the get() method didn't find any key called 'title' in x, so it returned None. However, you then try to slice it with [8:]. If it had returned a list or similar object it would work fine, but not so with None.

I recommend introducing some kind of error handling:

try:desired_value = [x.get('title')[8:] for x in desired_value]
except TypeError:return

You will have to correct and expand this stub to make it behave in a way that's appropriate for your program. Maybe instead of a return statement you'll need to define some kind of default desired_value or something.

https://en.xdnf.cn/q/118477.html

Related Q&A

RoboBrowser getting type error NoneType object is not subscriptable

Im trying to make a kahoot spammer which inputs a pin number and a username, decided by the user. Im getting a type error when I run this code:import re from robobrowser import RoboBrowser#Getting pin …

How to create a 2d list with all same values but can alter multiple elements within? (python)

Im trying to create a list that holds this exact format: [[2],[2],[2],[2],[2],[2],[2],[2],[2],[2]]and when list[3][0] = 9 is called, the list becomes [[2],[9],[2],[9],[2],[9],[2],[9],[2],[9]]How do I c…

Understanding Function Closures [duplicate]

This question already has answers here:Why arent python nested functions called closures?(10 answers)Closed 9 years ago.Im struggling to understand Function closures properly. For example in the code …

Update value for every row based on either of two previous columns

I am researching ATP Tour male tennis data. Currently, I have a Pandas dataframe that contains ~60,000 matches. Every row contains information / statistics about the match, split between the winner and…

Count consecutive equal values in array [duplicate]

This question already has answers here:Count consecutive occurences of values varying in length in a numpy array(5 answers)Closed 5 years ago.Say I have the following numpy array:a = np.array([1,5,5,2,…

how can I show please wait gif image before the process is complete

I want to show "please wait gif" image from img() class before the ListApp() class process is complete and then as soon as the process of that class is completed the screeen of ListApp should…

TypeError: list of indices must be integers, not str

What is wrong in my code to give me the error:TypeError: List of indices must be integers, not strHere is my code:print("This programe will keep track of your TV schedule.") Finish = False Sh…

Assignment in conditional not permitted in Python?

Why is code like if a = "hello":passinvalid in Python? The a = "Hello" is just a expression whose value is the Rvalue. Its valid in most languages like C or php. Some opinions?

Django - Join two Table without Foreign key

I have two tables and want to join them.. but I cant do that without rawQueryset and raw SQL. how can i join two models without foreign key? The columns for JOIN is not unique so it cant be PK and For…

Understanding lambda functions

Well I did try to read about Lambda functions but did not get across any link which explains few questions about its flow and the way it is handled by python interpretor or may be I could not understan…