通编码读取文件内容

通编码读取文件内容


# 通编码读取文件内容
def read_lines_from_file(file_path, coding="utf-8"):
    line_content = []
    if os.path.isfile(file_path):
        try:
            with open(file_path, encoding=coding) as fp:
                line_content = fp.readlines()
            return line_content
        except Exception as e:
            # print(e)
            try:
                with open(file_path, encoding="gbk") as fp:
                    line_content = fp.readlines()
                return line_content
            except Exception as e:
                print(e)
                return []
    elif os.path.isdir(file_path):
        print("%s is a dir! can not read content directly!" % file_path)
        return []
    else:
        print("%s file path does not exist!" % file_path)
        return []

升级

import os.path

def read_lines_from_file(file_path,coding="utf-8"):
    ‘‘‘此函数用于读取某个文件的所有行‘‘‘
    if os.path.isfile(file_path):
        #判断file_path参数是文件的情况
        try:
            #用utf-8编码去读取文件的所有行
            with open(file_path,encoding=coding) as fp:
                line_content = fp.readlines()
            return line_content
        except Exception as e:
            #print(e)
            #用utf-8编码读取出异常后,用gbk去读取文件的所有行
            try:
                with open(file_path,encoding="gbk") as fp:
                    line_content = fp.readlines()
                return line_content
            except Exception as e:
                print(e)
                return []
    elif os.path.isdir(file_path):
        #判断file_path参数是目录的情况
        print("%s is a dir! can not read content directly!" %file_path)
        return []
    else:
        #判断file_path参数即不是目录,也不是文件的情况
        print("%s file path does not exist!" %file_path)
        return []

#print(read_lines_from_file("e:\\笔记1.txt"))
#print(read_lines_from_file("e:\\test1111"))

def count_line_num(path,match_letters):
    """统计一个目录的包含某字符串的行数
       path参数可以是目录路径也可以是文件路径"""
    line_nums = 0

    if not os.path.exists(path):
        #判断路径在不在,不在的话返回0
        print("%s does not exists!" %path)
        return line_nums
    elif os.path.isfile(path):
        #当路径是文件的时候,用封装的read_lines_from_file
        #读取所有行,然后在做计数
        if ".txt" not in path:
            return line_nums
        for line in read_lines_from_file(path):
            if match_letters in line:
                line_nums+=1
        return line_nums
    elif os.path.isdir(path):
        #当路径是目录的时候,用封装的read_lines_from_file
        #读取所有行,然后在做计数
        for root,dirs,files in os.walk(path):
            for file in files:
                if ".txt" not in file:
                    continue
                file_path = os.path.join(root,file)
                for line in read_lines_from_file(file_path):
                    if match_letters in line:
                        line_nums+=1
        return line_nums

def get_specific_lines(path,match_letters):
    """统计一个目录的包含某字符串的所有行
       path参数可以是目录路径也可以是文件路径"""    

    specific_lines =[]
    if not os.path.exists(path):
        print("%s does not exists!" %path)
        return line_nums
    elif os.path.isfile(path):
        if ".txt" not in path:
            return line_nums
        for line in read_lines_from_file(path):
            if match_letters in line:
                specific_lines.append(line)
        return line_nums
    elif os.path.isdir(path):
        for root,dirs,files in os.walk(path):
            for file in files:
                if ".txt" not in file:
                    continue
                file_path = os.path.join(root,file)
                for line in read_lines_from_file(file_path):
                    if match_letters in line:
                        specific_lines.append(line)
        return specific_lines
#print(count_line_num("e:\\a.txt","ab"))
print(get_specific_lines("e:\\pic","ab"))

with  open(r"e:\result.txt",‘w‘) as fp:
    fp.writelines(get_specific_lines("e:\\pic","ab"))

再次修改:

import os.path

class Data:

    def __init__(self,path):
        self.path =path

    @staticmethod
    def  read_lines_from_file(file_path, coding="utf-8"):
        ‘‘‘此函数用于读取某个文件的所有行‘‘‘
        if os.path.isfile(file_path):
            # 判断file_path参数是文件的情况
            try:
                # 用utf-8编码去读取文件的所有行
                with open(file_path, encoding=coding) as fp:
                    line_content = fp.readlines()
                return line_content
            except Exception as e:
                # print(e)
                # 用utf-8编码读取出异常后,用gbk去读取文件的所有行
                try:
                    with open(file_path, encoding="gbk") as fp:
                        line_content = fp.readlines()
                    return line_content
                except Exception as e:
                    print(e)
                    return []
        elif os.path.isdir(file_path):
            # 判断file_path参数是目录的情况
            print("%s is a dir! can not read content directly!" % file_path)
            return []
        else:
            # 判断file_path参数即不是目录,也不是文件的情况
            print("%s file path does not exist!" % file_path)
            return []

    @staticmethod
    def read_lines_from_file(file_path, coding="utf-8"):
        ‘‘‘此函数用于读取某个文件的所有行‘‘‘
        if os.path.isfile(file_path):
            # 判断file_path参数是文件的情况
            try:
                # 用utf-8编码去读取文件的所有行
                with open(file_path, encoding=coding) as fp:
                    line_content = fp.readlines()
                return line_content
            except Exception as e:
                # print(e)
                # 用utf-8编码读取出异常后,用gbk去读取文件的所有行
                try:
                    with open(file_path, encoding="gbk") as fp:
                        line_content = fp.readlines()
                    return line_content
                except Exception as e:
                    print(e)
                    return []
        elif os.path.isdir(file_path):
            # 判断file_path参数是目录的情况
            print("%s is a dir! can not read content directly!" % file_path)
            return []
        else:
            # 判断file_path参数即不是目录,也不是文件的情况
            print("%s file path does not exist!" % file_path)
            return []

    @staticmethod
    def count_line_num(path, match_letters):
        """统计一个目录的包含某字符串的行数
           path参数可以是目录路径也可以是文件路径"""
        line_nums = 0

        if not os.path.exists(path):
            # 判断路径在不在,不在的话返回0
            print("%s does not exists!" % path)
            return line_nums
        elif os.path.isfile(path):
            # 当路径是文件的时候,用封装的read_lines_from_file
            # 读取所有行,然后在做计数
            if ".txt" not in path:
                return line_nums
            for line in Data.read_lines_from_file(path):
                if match_letters in line:
                    line_nums += 1
            return line_nums
        elif os.path.isdir(path):
            # 当路径是目录的时候,用封装的read_lines_from_file
            # 读取所有行,然后在做计数
            for root, dirs, files in os.walk(path):
                for file in files:
                    if ".txt" not in file:
                        continue
                    file_path = os.path.join(root, file)
                    for line in read_lines_from_file(file_path):
                        if match_letters in line:
                            line_nums += 1
            return line_nums

    @staticmethod
    def get_specific_lines(path, match_letters):
        """统计一个目录的包含某字符串的所有行
           path参数可以是目录路径也可以是文件路径"""

        specific_lines = []
        if not os.path.exists(path):
            print("%s does not exists!" % path)
            return specific_lines
        elif os.path.isfile(path):
            if ".txt" not in path:
                return []
            for line in Data.read_lines_from_file(path):
                if match_letters in line:
                    specific_lines.append(line)
            return specific_lines
        elif os.path.isdir(path):
            for root, dirs, files in os.walk(path):
                for file in files:
                    if ".txt" not in file:
                        continue
                    file_path = os.path.join(root, file)
                    for line in read_lines_from_file(file_path):
                        if match_letters in line:
                            specific_lines.append(line)
            return specific_lines

print(Data.read_lines_from_file("e:\\a.txt"))
print(Data.count_line_num("e:\\a.txt","ab"))
print(Data.get_specific_lines("e:\\a.txt","ab"))

原文地址:https://blog.51cto.com/357712148/2367952

时间: 2024-10-13 16:04:30

通编码读取文件内容的相关文章

Java实现一行一行读取文件内容(进行编码处理)

// 读取文件内容public String readFile(){ String path = ""; File file = new File(path); StringBuilder result = new StringBuilder(); try{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));//构造一个B

7 RandomAccessFile读取文件内容保存--简单例子(需要验证)

1 import org.slf4j.Logger; 2 import org.slf4j.LoggerFactory; 3 4 import java.io.*; 5 6 /** 7 * 读取动态产生的文件内容 8 */ 9 public class RandomAccessRead { 10 public static Logger logger= LoggerFactory.getLogger(RandomAccessRead.class); 11 12 //文件默认读取位置为从开始读取

B.php中读取文件内容的几种方法

php中读取文件内容的几种方法 1.fread string fread ( int $handle , int $length ) fread() 从 handle 指向的文件中读取最多 length 个字节.该函数在读取完最多 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192 个字节时就会停止读取文件,视乎先碰到哪种情况. fread() 返回所读取的字符串,如果出错返回 FALSE. <?php $filename

asp.net 上传XML,txt 直接读取文件内容

if (GetUploadFileContent.PostedFile.InputStream.Length < 1) { Msg.Text = "请选择文件";return; } string FileName = GetUploadFileContent.FileName;//上传文件文件名 string FilePath = GetUploadFileContent.PostedFile.FileName;//上传文件完整路径+文件名string fileExtName =

PHP读取文件内容的五种方式

php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp);-- php读取文件内容: -----第一种方法-----fread()-------- ? 1 2 3 4 5 6 7 8 <?php $file_path = "test.txt"; if(file_exists($file_path)){ $fp = fopen($file_path,"r"); $str

使用ifstream和getline读取文件内容[c++]

转载:http://www.cnblogs.com/JCSU/articles/1190685.html 假设有一个叫 data.txt 的文件, 它包含以下内容: Fry: One Jillion dollars. [Everyone gasps.] Auctioneer: Sir, that's not a number. 数据读取, 测试 . 以下就是基于 data.txt 的数据读取操作: #include <iostream> #include <fstream> #in

php读取文件内容的三种方式(转)

分享下php读取文件内容的三种方法. php读取文件内容: //**************第一种读取方式***************************** header("content-type:text/html;charset=utf-8"); //文件路径 $file_path="text.txt"; //判断是否有这个文件 if(file_exists($file_path)){ if($fp=fopen($file_path,"a+&

Python逐行读取文件内容

Python逐行读取文件内容thefile= open("foo.txt") line = thefile.readline() while line: print line, line = thefile.readline() thefile.close() Windows下文件路径的写法:E:/codes/tions.txt 写文件:thefile= open("foo.txt", "rw+")for item in thelist: the

nodejs常见的读取文件内容的方法

nodejs常见的读取文件内容的方法 by 伍雪颖 var fs = require('fs'); var rs = fs.createReadStream('test.md'); var data = ''; rs.on("data",function(chunk) { data += chunk; }); rs.on("end",function() { console.log(data); });