IndexError - 两次附加到列表
IndexError - Appending to a list twice
我的以下代码出现错误。我文件中的第三个索引是网络服务器日志的时间。我想将它附加到空列表中。
无论使用什么信息,我尝试附加的第二个值总是会出现此错误。有什么建议可以解决这个问题吗?
代码:
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)
- 确切错误:
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
- Nasa 文本文件的示例行
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
你可以根据你的数据写这样的东西。您的输入在开头和结尾有几个换行符。如果您也想包括它,您可能需要在附加之前检查 if len(split_data) > 1
。
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)
我的以下代码出现错误。我文件中的第三个索引是网络服务器日志的时间。我想将它附加到空列表中。 无论使用什么信息,我尝试附加的第二个值总是会出现此错误。有什么建议可以解决这个问题吗? 代码:
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)
- 确切错误:
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
- Nasa 文本文件的示例行
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
你可以根据你的数据写这样的东西。您的输入在开头和结尾有几个换行符。如果您也想包括它,您可能需要在附加之前检查 if len(split_data) > 1
。
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)