程序员文章、书籍推荐和程序员创业信息与资源分享平台

网站首页 > 技术文章 正文

python读取文件

hfteth 2025-01-06 21:46:43 技术文章 12 ℃

python批量读取文件,一行一行输出并去除换号符

输出字符串数据类型

import sys
with open("test.log", 'r', encoding='utf-8') as f:
    while True:
        rsize = 1024*1024*1                                 #每次读取1M大小
        lines = f.readlines(rsize)
        if not lines:
           break
        for line in lines:
            message = line.replace("\n", "")               
            print(message)

python一行一行读取文件,一行一行输出并去除换号符

输出列表数据类型

with open('test.log', 'r', encoding = 'utf-8') as f:
    while True:
        line = f.readline() 
        if not line: 
            break
        message = line.split('\n')
        print(message)

Tags:

最近发表
标签列表