简体   繁体   中英

How to read the two columns of data in this text file?

I have a text file which include some numbers and words as shown below.

How to read first column and second column?

After reading the file, I need to get an average value of the first column.

33512 test1
3312  test2
551   test3
33    test4
5     test5
vals = []

with open('filename.txt', 'r') as f:
    for line in f:
        vals.append(line.split()[0:2])

numbers = [val[0] for val in vals]
avg = sum(numbers) / len(numbers)     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM