栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Python中从CSV文件读取行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Python中从CSV文件读取行

您可以执行以下操作:

with open("data1.txt") as f:    lis = [line.split() for line in f]        # create a list of lists    for i, x in enumerate(lis):   #print the list items         print "line{0} = {1}".format(i, x)# output line0 = ['Year:', 'Dec:', 'Jan:']line1 = ['1', '50', '60']line2 = ['2', '25', '50']line3 = ['3', '30', '30']line4 = ['4', '40', '20']line5 = ['5', '10', '10']

要么 :

with open("data1.txt") as f:    for i, line in enumerate(f):          print "line {0} = {1}".format(i, line.split())# output         line 0 = ['Year:', 'Dec:', 'Jan:']line 1 = ['1', '50', '60']line 2 = ['2', '25', '50']line 3 = ['3', '30', '30']line 4 = ['4', '40', '20']line 5 = ['5', '10', '10']

编辑:

with open('data1.txt') as f:    print "{0}".format(f.readline().split())    for x in f:        x = x.split()        print "{0} = {1}".format(x[0],sum(map(int, x[1:])))# output          ['Year:', 'Dec:', 'Jan:']1 = 1102 = 753 = 604 = 605 = 20


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/638662.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号