python2处理表格文件按照规则多行输出(中文路径,print)

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 #python2支持print带入参
 5 from __future__ import print_function
 6 # python2使用utf-8编码解决中文路径问题
 7 from __future__ import unicode_literals 
 8 import os, sys, json, getopt, re
 9 #在python3中不需要设置也可以支持中文路径
10 reload(sys) 
11 sys.setdefaultencoding('utf-8')
12 
13 class printTableFunc(object):
14     def __init__(self, file, fileList = [], fileDir = {}):
15         self.process(file, fileList, fileDir)
16                    
17     def process(self, file, fileList, fileDir):
18         with open(file, 'rw') as fr:
19             # print fr.name
20             data = fr.readline()
21             print ("//"+data)
22             lines = fr.readlines()
23             for line in lines:
24                 line = line.strip('\n\r')
25                 # print line
26                 fileList.append('{'+line+'}, ')
27         fr.close()
28 
29 if __name__ == '__main__':
30     tableFile = '/测试中文路径/test.csv'
31     fileList = []
32     fileDir = {}
33 
34     printTableFunc(tableFile, fileList, fileDir)
35     i = 0
36     for file in fileList:
37         i = i + 1
38         if (0 == i % 7):
39             print(file)
40         else:
41             print(file, end='')

 以上代码风格有点C,后面再写一个正则表达式的实现练习一下,mark一下20210623

posted @ 2021-06-21 10:09  Lunais  阅读(195)  评论(0编辑  收藏  举报