Python解析CSV
2018/10/12 10:56 分类: 技术交流 浏览:40
- 1. 学科编号,学科,开班时间,预计人数
- 2. 1,Java,2018/8/9,50
- 3. 2,PHP,2018/8/10,51
- 4. 3,UI,2018/8/11,52
- 5. 4,H5,2018/8/12,53
- 6. 5,Python,2018/8/13,54
1. import csv
2.
3. DATA = (
4. (u'学科编号', u'学科', u'开班时间', u'预计人数'),
5. (u'1', u'Java', u'2018/8/9', u'50'),
6. ('2', 'PHP', '2018/8/10', '51'),
7. ('3', 'UI', '2018/8/11', '52'),
8. ('4', 'H5', '2018/8/12', '53'),
9. ('5', 'Python', '2018/8/13', '54'),
10. )
11.
12. print('*** 保存CSV数据')
13. f = open('xueke.csv', 'w', newline='')
14. writer = csv.writer(f)
15. for record in DATA:
16. writer.writerow(record)
17. f.close()
18.
19. print('*** 读取CSV数据')
20. f = open('xueke.csv', 'r', newline='')
21. reader = csv.reader(f)
22. for no, name, time, num in reader:
23. print('%s %s %s %s' % (no, name, time, num))
24. f.close()
赞 0