本文摘自php中文网,作者黄舟,侵删。
本文通过代码给大家介绍了Python 逐行分割大txt文件的方法,在文中给大家提到了Python从txt文件中逐行读取数据的方法,需要的朋友参考下吧代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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文件中逐行读取数据
非常的简单,提供三种方法:
方法一:
1 2 3 4 5 6 7 8 | f = open ( "foo.txt" )
line = f.readline()
while line:
print line,
line = f.readline()
f.close()
|
方法二:
1 2 | for line in open ( "foo.txt" ):
print line,
|
方法三:
1 2 3 4 | f = open ( "c:\\1.txt" , "r" )
lines = f.readlines()
for line in lines
print line
|
总结
以上就是Python实现逐行分割大txt文件的方法介绍的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python end用法是什么?
Python生成器generator介绍
如何来理解Python中的进程和线程?
Python传递方式是怎样的?速度本文了解Python参数传递
Python全栈指的是什么
使用 if x is not none 还是if not x is none
Python爬虫是什么?为什么把Python叫做爬虫?
Python中迭代器和生成器的示例详解
Python创建于英国吗
Python实现微信推送模板消息功能示例
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python实现逐行分割大txt文件的方法介绍