Python: 常用文件处理功能及其他。。。

#regex replace character in string
re.sub(r"[():;,]"," ",line)

#check whether a string is an existed file
if os.path.isfile(fldPnNanmeValueFile) is not True:
   continue

# print with paramters
print("original xml file: %s \n"%xmlName)
 
#write to file
f3 = open(resultXmlFile, 'w',encoding="utf8")
for line in newLines:
  f3.write(line)
f3.close();
#读取tab 分隔的txt文件:
#例如: test.txt :
#     line1: key1\tkey2     
#     line2: value1\value2
#方法1: 使用CSV module
       import csv
       linesList =  list(csv.reader(open(filePath,encoding="xx"), delimiter='\t'))
       #lineList 的内容: [['key1', 'key2'], ['value1', 'value2']]

#统计文件非空行行数
lineCount = sum(1 for line in open(filePath, encoding='xx') if line.strip())
#读取文件中所有的非空行,note: \n will be kept in this way
lines = [line for line in open(filePath, encoding='xx') if line.strip()]

#sort 二维数组,并以[][1]为key, 例如dim2List = [[‘a’, 4], ['c',2]],
#after sort will be [['c', 2], ['a',4]]
dim2List = sorted(dim2List , key=lambda x:x[1])
#列出rootDir下的一级子目录
subDirs = [name for name in os.listdir(rootDir) if os.path.isdir( os.path.join(rootDir, name) )]
#an ugly way to open files either encoded in utf8 or utf16
    try: 
        lines =  [line for lines in open(filePath, encoding="utf-16")]
    except UnicodeError:
        try:
            lines =  [line for lines in open(filePath, encoding="utf-8")]
        except:
            print ("Could not read file: %s", filePath)
            sys.exit()
    except:
        print ("Could not read file: %s", filePath)
        sys.exit()

 


 

posted @ 2016-01-07 16:21  鱼悠游  阅读(187)  评论(0编辑  收藏  举报