- 论坛徽章:
- 0
|
本帖最后由 anonymous0502 于 2012-05-24 09:29 编辑
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import sys
- from pprint import pprint
- def myprint(obj, end='\n'):
- sys.stdout.write(str(obj) + end)
- def loadtxtintotable(fname, rows, skipemptyline=False):
- result = [list() for i in range(rows)]
- i = 0
- with open(fname, 'r') as f:
- while True:
- line = f.readline()
- if not line:
- break
- if skipemptyline and line.strip() == '':
- continue
- result[i % rows].append(line.strip())
- i += 1
- return result
- def main():
- fname = 'c:/temp/1.txt'
- rows = 5
- skipemptyline = True
- tablerows = loadtxtintotable(fname, rows, skipemptyline)
- pprint(tablerows)
- if __name__ == '__main__':
- main()
- myprint('done')
复制代码 |
|