Python 逐行分割大txt檔案的方法

2022-10-04 20:48:21 字數 1339 閱讀 4302

**如下所示:

# -*- coding: -*-

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_listwww.cppcns.com=

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 www.cppcns.comin url_list:

file.write(url)

print('done')

python從檔案中逐行讀取資料

非常的簡單,提供三種方法:

方法一:

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檔案的方法

本文位址:

python逐行讀寫txt檔案

coding utf 8 import os file obj open test2.txt all lines file obj.readlines for line in all lines print line file obj.close 寫之前,先檢驗檔案是否存在,存在就刪掉 if os....

Python從txt檔案中逐行讀取資料

coding utf 8 import os for line in open samples label val.txt print line line,end 後面跟 end 將忽略換行符 line samples images 3 3 5460e99f0ca9c410960571e02a0d2...

python逐行讀取txt檔案時出現多餘空行的問題

這幾天做程式作業的時候需要用python的讀取檔案功能,在我用readlines 函式做逐行讀取的時候遇到了乙個小問題,在這裡和大家分享一下。txt檔案裡的內容是這樣的 1 1 2 23 3 4 45 5 也沒什麼問題 1 with open 001.txt r as f 2 lines f.rea...