如何用Python编程实现自动切分文本文件中的多行内容?

2026-05-26 20:051阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计503个文字,预计阅读时间需要3分钟。

如何用Python编程实现自动切分文本文件中的多行内容?

针对配置文件进行分割,重组,每30行作为一段,生成功能。代码如下:

pythondef split_and_reorganize(file_path): # 读取文件内容 with open(file_path, 'r') as file: lines=file.readlines()

# 每30行分割 chunks=[lines[i:i+30] for i in range(0, len(lines), 30)]

# 重组每段内容 reorganized_chunks=[] for chunk in chunks: # 去除每行末尾的换行符 chunk=[line.strip() for line in chunk] # 拼接成一段新的内容 reorganized_chunk='\n'.join(chunk) reorganized_chunks.append(reorganized_chunk)

# 返回重组后的内容 return reorganized_chunks

示例使用file_path='config.txt' # 配置文件路径reorganized_content=split_and_reorganize(file_path)

打印结果for content in reorganized_content: print(content)

针对配置文件进行切分,重组,每隔30行为一段,进行重新生成功能。

代码如下

#!/usr/local/python/bin/python # coding=utf-8 import sys import re import os f = open('config.conf','r') #判断文件条数 def file_num(filename): num_col = 0 with open(filename,'rb') as Fnum: while(Fnum.readline() !=''): num_col = num_col + 1 return num_col file_num = file_num('config.conf') print '文件总条数:' + str(file_num) i = 0 #设置计数器 while i < file_num : #表示文件行数 with open('/tmp/newfile/newfile'+str(i),'w') as f1: for j in range(0,30) : #这里设置每个子文件的大小 if i <= file_num : #这里判断是否已结束,否则最后可能报错 f1.writelines(f.readline()) i = i+1 else: break filepath1='/tmp/newfile/' def eachFile(filepath): num = 1 pathDir = sorted(os.listdir(filepath)) for allDir in pathDir: child = os.path.join('%s%s' % (filepath, allDir)) file_name = child.decode('gbk') print file_name with open('/tmp/check.py','a+') as f2: fopen = open(file_name, 'r+') f2.writelines("# config.py sql" + str(num) + " begin\n") count=0 for line in fopen.readlines(): if count == 0: f2.writelines(line) count= count+1 else: f2.writelines("UNION ALL " + line) f2.writelines("# config.py sql" + str(num) + " end\n") f2.writelines("\n") num = num + 1 eachFile(filepath1)

脚本说明:

目录路径:/tmp

配置文件:config.conf

准备目录:/tmp/newfile/

最终新的配置文件为:check.py

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

如何用Python编程实现自动切分文本文件中的多行内容?

本文共计503个文字,预计阅读时间需要3分钟。

如何用Python编程实现自动切分文本文件中的多行内容?

针对配置文件进行分割,重组,每30行作为一段,生成功能。代码如下:

pythondef split_and_reorganize(file_path): # 读取文件内容 with open(file_path, 'r') as file: lines=file.readlines()

# 每30行分割 chunks=[lines[i:i+30] for i in range(0, len(lines), 30)]

# 重组每段内容 reorganized_chunks=[] for chunk in chunks: # 去除每行末尾的换行符 chunk=[line.strip() for line in chunk] # 拼接成一段新的内容 reorganized_chunk='\n'.join(chunk) reorganized_chunks.append(reorganized_chunk)

# 返回重组后的内容 return reorganized_chunks

示例使用file_path='config.txt' # 配置文件路径reorganized_content=split_and_reorganize(file_path)

打印结果for content in reorganized_content: print(content)

针对配置文件进行切分,重组,每隔30行为一段,进行重新生成功能。

代码如下

#!/usr/local/python/bin/python # coding=utf-8 import sys import re import os f = open('config.conf','r') #判断文件条数 def file_num(filename): num_col = 0 with open(filename,'rb') as Fnum: while(Fnum.readline() !=''): num_col = num_col + 1 return num_col file_num = file_num('config.conf') print '文件总条数:' + str(file_num) i = 0 #设置计数器 while i < file_num : #表示文件行数 with open('/tmp/newfile/newfile'+str(i),'w') as f1: for j in range(0,30) : #这里设置每个子文件的大小 if i <= file_num : #这里判断是否已结束,否则最后可能报错 f1.writelines(f.readline()) i = i+1 else: break filepath1='/tmp/newfile/' def eachFile(filepath): num = 1 pathDir = sorted(os.listdir(filepath)) for allDir in pathDir: child = os.path.join('%s%s' % (filepath, allDir)) file_name = child.decode('gbk') print file_name with open('/tmp/check.py','a+') as f2: fopen = open(file_name, 'r+') f2.writelines("# config.py sql" + str(num) + " begin\n") count=0 for line in fopen.readlines(): if count == 0: f2.writelines(line) count= count+1 else: f2.writelines("UNION ALL " + line) f2.writelines("# config.py sql" + str(num) + " end\n") f2.writelines("\n") num = num + 1 eachFile(filepath1)

脚本说明:

目录路径:/tmp

配置文件:config.conf

准备目录:/tmp/newfile/

最终新的配置文件为:check.py

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

如何用Python编程实现自动切分文本文件中的多行内容?