Home >Backend Development >Python Tutorial >Introduction to the method of splitting large txt files line by line in Python

Introduction to the method of splitting large txt files line by line in Python

黄舟
黄舟Original
2017-10-10 10:49:223719browse

本文通过代码给大家介绍了Python 逐行分割大txt文件的方法,在文中给大家提到了Python从txt文件中逐行读取数据的方法,需要的朋友参考下吧

代码如下所示:


# -*- coding: <encoding name> -*-
import io
LIMIT = 150000
file_count = 0
url_list = []
with io.open(&#39;D:\DB_NEW_bak\DB_NEW_20171009_bak.sql&#39;,&#39;r&#39;,encoding=&#39;utf-16&#39;) 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,&#39;w&#39;,encoding=&#39;utf-16&#39;) 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,&#39;w&#39;,encoding=&#39;utf-16&#39;) as file:
    for url in url_list:
      file.write(url)
print(&#39;done&#39;)

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

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

方法一:


f = open("foo.txt")       # 返回一个文件对象 
line = f.readline()       # 调用文件的 readline()方法 
while line: 
  print line,         # 后面跟 &#39;,&#39; 将忽略换行符 
  # print(line, end = &#39;&#39;)   # 在 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

总结

The above is the detailed content of Introduction to the method of splitting large txt files line by line in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn