csv读取

  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import string
  5. newline, s = ("", "")
  6. try:
  7. f = open(u"简易记账.csv")
  8. except IOError:
  9. print "文件不存在!".decode('utf8').encode('gbk')
  10. sys.exit()
  11. f1 = open(u"结果.txt", 'w')
  12. total, n = (0, 1)
  13. for line in f:
  14. if n == 1:
  15. n += 1
  16. continue
  17. mylist = line.decode('utf8').encode('gbk').split(',')
  18. del mylist[0:4]
  19. del mylist[2]
  20. date = mylist[0][0:10]
  21. date = date.replace('-', '')
  22. money = float(mylist[1])
  23. total += money
  24. s = '{0} {1:>3.0f} {2}'.format(date, money, mylist[2])
  25. newline += s
  26. newline += '{0:>8s} {1:>3.0f}'.format("合计:".decode('utf8').encode('gbk'), total)
  27. f1.write(newline)
  28. f1.close()
  29. f.close()


raw_input("请按任意键继续!".decode('utf8').encode('gbk'))
每次都要解码




posted @ 2015-02-06 16:48  阳光树林  阅读(221)  评论(0编辑  收藏  举报