Search code examples
pythonappendindex-error

IndexError - Appending to a list twice


I am getting an error for my code below. The 3rd index in my file is the times for a web server log. I want to append it to the empty list. The second value I try to append always gets this error, regardless of what information is used. Any suggestions to fix this? Code:

nasa = open('NASA_access_log_Jul95')
import statistics

dns = []
loginTime = []

for lineOne in nasa:
    split_data = lineOne.split(" ")
    dns.append(split_data[0])
    loginTime.append(split_data[3])

freq = statistics.mode(dns)
least = min(dns)

print(freq)
print(loginTime)

- Exact Error:

Traceback (most recent call last):
  File "/Users/technomancer/PycharmProjects/Akhters_DATA_Parsing/main.py", line 12, in <module>
    loginTime.append(split_data[3])
IndexError: list index out of range

- Sample Lines for Nasa Text File

199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] "GET /history/apollo/ HTTP/1.0" 200 6245
unicomp6.unicomp.net - - [01/Jul/1995:00:00:06 -0400] "GET /shuttle/countdown/ HTTP/1.0" 200 3985
199.120.110.21 - - [01/Jul/1995:00:00:09 -0400] "GET /shuttle/missions/sts-73/mission-sts-73.html HTTP/1.0" 200 4085

Solution

  • You can write something like this based on your data. Your input has couple of new line characters at the beginning and end. If you want to include that as well, you may want to check if len(split_data) > 1 before appending.

    nasa = open('NASA_access_log_Jul95')
    import statistics
    
    dns = []
    loginTime = []
    
    for line in nasa.read().split("\n"):
         split_data = line.split(" ")
         if len(split_data) > 1:
             dns.append(split_data[0])
             loginTime.append(split_data[3])
    
    freq = statistics.mode(dns)
    least = min(dns)
    
    print(freq)
    print(loginTime)