关于python

打开csv

# -*- coding: utf8 -*-import csv
with open(‘test.csv’,’rb’) as my:
lines=csv.reader(my)
for line in lines:
print line

# -*- coding: utf8 -*-import csv
with open(‘text01.csv’,’wb’) as my:
mywriter=csv.writer(my)
mywriter.writerow([1,’a’])
mywriter.writerow([2,’b’])
lists=[[3,5],[4,6]]
mywriter.writerows(lists)

# -*- coding: utf8 -*-import csv
with open(‘text01.csv’,’rb’) as my:
lines=csv.reader(my,csv.register_dialect(‘mydialect’,delimiter=’|’, quoting=csv.quote_all))
print lines.line_num
for line in lines:
print line

# -*- coding: utf8 -*-import csv
with open(‘text.csv’, ‘wb’) as csvfile:
spamwriter = csv.writer(csvfile, delimiter=’ ‘,
quotechar=’|’, quoting=csv.quote_minimal)
spamwriter.writerow([‘tiger’] * 5 + [‘pig ‘])
spamwriter.writerow([‘cat’, ‘mouse’, ‘dog’])

以上就是关于python-csv的使用介绍的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论