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()




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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭