babynames2000s.txt(неполный):
1 Jacob 273,450 Emily 223,488
2 Michael 250,174 Madison 192,976
3 Joshua 231,602 Emma 181,055
4 Matthew 221,266 Olivia 155,848
5 Daniel 203,404 Hannah 155,525
6 Christopher 203,006 Abigail 150,746
7 Andrew 202,208 Isabella 149,310
8 Ethan 201,611 Samantha 134,148
9 Joseph 194,377 Elizabeth 133,166
10 William 194,079 Ashley 132,934
в первом столбце находятся мужские имена. задача записывать в новый лист boysfile элементы из listfile начиная с первого индекса и далее через 3.
infile = open("babynames2000s.txt", "r")
outfile_boys = open("boynames2014.txt", "w")
outfile_girls = open("girlnames2014.txt", "w")
listfile = []
lines = infile.read().split('\t')
indexx = 1
boysfile = []
for line in lines:
listfile.append(line.rstrip())
print(listfile[indexx])
for item in listfile:
boysfile = listfile[indexx]
indexx = indexx + 3
outfile_boys.write(boysfile)
print(boysfile)
выдаёт:
Jacob
Traceback (most recent call last): File "C:/Users/spart/Documents/python/homework4/boys_and_girls.py", line 15, in boysfile = listfile[indexx]
IndexError: list index out of range
так же пытался вместо boysfile = listfile[indexx]
, использовать boysfile.append(listfile[indexx])
выдавало ту же самую ошибку