• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2062
  • Last Modified:

Index Error: List index out of range

i have a PYTHON script that parses data from an .rdf file. Everything is working great except one field / one section of code that I really could use help on. The data field is called PCIT and it looks like this in the .rdf file>>   ..PCIT:      US20040064316 20040401            <The errror that i receive is :

File "textgen.py", line 153 in generate
     yyyymmdd = fields[1]
IndexError: list index out of range

here is the code from the textgen.py file

if ( (dict_key == 'PCIT') and dict_val ):
fields = dict_val.split()
del fields[-1]
yyyymmdd = fields[1]
yyyy          = int(yyyymmdd[0:4])
mm             = int(yyyymmdd[4:6])
if mm < 1: mm = 1
print "YYYY", yyyy, "MM", mm
do = mx.DateTime.Date(yyyy, mm, 1)
fields[1] = do.strftime("%b %y")
dict_val = " ".join(fields)

Open in new window

  • 3
1 Solution
If the 'US20040064316 20040401' is get in dict_val, then it is split to two-elements list ['US20040064316', '20040401']. Then

del fields[-1]

removes the last element and the fields is equal to ['US20040064316']. The only element can be indexed by zero, not by 1.
Possibly, the dict_val does not contain what you expect. Try to use the following command to clearly visualize the content of a string variable:

print repr(dict_val)

The repr() built-in function returns the string that could be used to express the same value as a string literal in your source. (Well, complicated explanation -- easy to understand when you try.)
Shaggy1Author Commented:
Ok, i used the print repr(dict_val) and value it returned was:

'DE2914336 19801106'

This is a different record ref above, but of course the same issue. So, what i need is the date portion of this value which seems to be the 19801106. So instead of
del fields[-1] i should be using [1] ?
and how would that affect the rest of the code....
I am a newbie, but i understand everything you are saying so far....thanks for the help!
The fields is the identifier for the Python list which is internally implemented as dynamic array of references to the values (the objects with the values). It can be indexed from zero.

The .split() method of the built in string returns the list of parts of the string. With default arguments, it splits by spaces and whitespace characters.

The    fields = 'DE2914336 19801106'.split() returns the list ['DE2914336',  '19801106'] and associates it with name "fields".

Then fields[0] is equal to 'DE2914336', and the fields[1] is equal to '19801106'. The fields[-1] is a Python syntax for indexing from the end of the list. Here it is the last element of the list.

The del fields[-1] removes the last element of the list. Then fields has the only element that can be accesed as fields[0]... or also fields[-1] as the last element.

This way, you may want not to use del or you want to change the index to 0 when accessing the element. Based on decision, you loose the last element or you can use it later.

You can also do:

     first, second = dict_val.split()

if you are sure that there are at least two elements. This way the elements are assigned to the variables first and second respectively.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now