fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for x in fh:
x = x.rstrip()
wds = x.split()
if wds[0] == 'From ':
print(wds[1])
count = count + 1
else:
continue
print("There were", count, "lines in the file with From as the first word")
https://www.edwith.org/python-data/lecture/24392/
일단 여기서 강의들으면서 하고있는데
if wds[0] == 'From ':
이 부분에서
Traceback (most recent call last):
File "C:\Users\qqqq\Desktop\test.py", line 10, in <module>
if wds[0] == 'From ':
IndexError: list index out of range
이런 에러가 자꾸 뜸...
내가 하려는건 메일주소가 포함된 문서파일에서 메일주소 뽑아내는거거든?
근데 왜 에러나는거임 ㅠㅠ 도와죠
txt 파일에 From 이 없는거 아니냐
있슴 txt파일 주소는
https://www.py4e.com/code3/mbox-short.txt
Wds에 저장이 안된거면 x쪽 문제 아닐까여 ㄷ x값 확인 한번 해보셈
텍스 파일에 중간중간 빈줄 있잖아 그러면 x가 개행문자만포함된 문자열이 되고 rstrip 하면 빈 문자열 되고 그걸 split하면 빈 리스트 됨
x를 split하기 전에 x가 빈 문자열인지를 먼저 체크해야함