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

前端 未结 6 1248
佛祖请我去吃肉
佛祖请我去吃肉 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
    
    0 讨论(0)
  • 2021-02-04 23:10

    l=[] with open ('task-1.txt', 'rt') as myfile:

    for x in myfile:                
        l.append(x)
    

    for i in l: print[i.split()[0] ]

    0 讨论(0)
  • 2021-02-04 23:17

    I'd go for the str.split and similar approaches, but for completness here's one that uses a combination of mmap and re if you needed to extract more complicated data:

    import mmap, re
    
    with open('quizzes.txt') as fin:
        mf = mmap.mmap(fin.fileno(), 0, access=mmap.ACCESS_READ)
        wordlist = re.findall('^(\w+)', mf, flags=re.M)
    
    0 讨论(0)
  • 2021-02-04 23:18

    Changed to a one-liner that's also more efficient with the strip as Jon Clements suggested in a comment.

    with open('Quizzes.txt', 'r') as f:
        wordlist = [line.split(None, 1)[0] for line in f]
    

    This is pretty irrelevant to your question, but just so the line.split(None, 1) doesn't confuse you, it's a bit more efficient because it only splits the line 1 time.

    From the str.split([sep[, maxsplit]]) docs

    If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

    ' 1 2 3 '.split() returns ['1', '2', '3']

    and

    ' 1 2 3 '.split(None, 1) returns ['1', '2 3 '].

    0 讨论(0)
  • 2021-02-04 23:19

    You are using split at the wrong point, try:

    for line in f:
        QuizList.append(line.split(None, 1)[0]) # add only first word
    
    0 讨论(0)
  • 2021-02-04 23:20
    with Open(filename,"r") as f:
        wordlist = [r.split()[0] for r in f]
    
    0 讨论(0)
提交回复
热议问题