python 19th days [list comprehension & File IO]

版权声明:open source https://blog.csdn.net/Smith_My_/article/details/79946164
# -*- coding: utf-8 -*-
"""
Spyder Editor


This is a temporary script file.
"""
line = '1 2 3 4 5'
fields = line.split()
total = 0
for field in fields:
    total += int(field)
print(total)


sigma = sum([int(field) for field in fields]) #list comprehension  列表推导式

print(sigma)

------------------------------------------------------------------------------------

# -*- coding: utf-8 -*-
"""
Spyder Editor


This is a temporary script file.
"""
# File I/O
'''
f = open('datacsv.txt', 'w')
f.write('tonight I wanna be with u \n')
f.write('I am a little red and little blue \n')
f.close()
'''
f = open('datacsv.txt','r+')
#data =f.read()
#print(data)
data = []
for line in f:
    data.append([str(field) for field in line.split()])


print(data)
for row in data:
    print(row)
    #for single in row:
        #print(single)
        
f.close()




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页