#!/usr/bin/env python
# encoding: utf-8
"""
@author: wugang
@contact: 752820344@qq测试数据
@software: PyCharm
@file: toolkits_file.py
@time: 2017/3/1 0001 17:01
"""
'''
对文件操作的工具模块
'''
# 1.将A文件复制到B文件中去(保持原来格式)
def copy_file (inputFile, outputFile, encoding):
fin = open(inputFile, 'r', encoding=encoding) #以读的方式打开文件
fout = open(outputFile, 'w', encoding=encoding) #以写得方式打开文件
for eachLiine in fin.readlines(): #读取文件的每一行
line = eachLiine.strip() #去除每行的首位空格
fout.write(line + '\n')
fin.close()
fout.close()
# 2. 读取文件中的内容,返回List列表 (加载本地词典库)
def read_file_list(inputFile, encoding):
results = []
fin = open(inputFile, 'r', encoding=encoding)
for eachLiine in fin.readlines():
line = eachLiine.strip().replace('\ufeff', '')
results.append(line)
fin.close()
return results
# 3.读取文件,返回文件内容
def read_file(path):
with open(path, 'r+', encoding='UTF-8') as f:
str = f.read()
return str.strip().replace('\ufeff', '')
def func():
pass
if __name__ == '__main__':
copy_file('data/test1.txt', 'data/text.txt','UTF-8')
contents = read_file_list('dict/time.dict','UTF-8')
print(contents) 相关推荐:
python读取文本数据并转化为DataFrame格式的方法详解
python读取word中的文本内容
以上就是Python 3.6 读取并操作文件内容的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于Python3.6读取并操作文件内容的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did84078