欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python 逐行分割大txt文件的方法

程序员文章站 2024-02-24 12:20:18
代码如下所示: # -*- coding: -*- import io LIMIT = 150000 file_...

代码如下所示:

# -*- coding: <encoding name> -*-
import io
LIMIT = 150000
file_count = 0
url_list = []
with io.open('D:\DB_NEW_bak\DB_NEW_20171009_bak.sql','r',encoding='utf-16') as f:
  for line in f:
    url_list.append(line)
    if len(url_list) < LIMIT:
      continue
    file_name = str(file_count)+".sql"
    with io.open(file_name,'w',encoding='utf-16') as file:
      for url in url_list[:-1]:
        file.write(url)
      file.write(url_list[-1].strip())
      url_list=[]
      file_count+=1
if url_list:
  file_name = str(file_count) + ".sql"
  with io.open(file_name,'w',encoding='utf-16') as file:
    for url in url_list:
      file.write(url)
print('done')

Python从txt文件中逐行读取数据

非常的简单,提供三种方法:

方法一:

f = open("foo.txt")       # 返回一个文件对象 
line = f.readline()       # 调用文件的 readline()方法 
while line: 
  print line,         # 后面跟 ',' 将忽略换行符 
  # print(line, end = '')   # 在 Python 3中使用 
  line = f.readline() 
 
f.close() 

方法二:

for line in open("foo.txt"): 
  print line, 

方法三:

f = open("c:\\1.txt","r") 
lines = f.readlines()#读取全部内容 
for line in lines 
  print line 

总结

以上所述是小编给大家介绍的Python 逐行分割大txt文件,希望对大家有所帮助