How would I read only the first word of each line of a text file?

前端 未结 6 1266
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 22:17

I wanted to know how I could read ONLY the FIRST WORD of each line in a text file. I tried various codes and tried altering codes but can only manage to read whole lines from a

6条回答
  •  旧巷少年郎
    2021-02-04 22:58

    You should read one character at a time:

    import string
    
    QuizList = []
    with open('Quizzes.txt','r') as f:
        for line in f:
            for i, c in enumerate(line):
                if c not in string.letters:
                    print line[:i]
                    break
    

提交回复
热议问题