文本处理和文件创建 [英] Text processing and file creation

查看:71
本文介绍了文本处理和文件创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约20,000行的文本源文件。


>从这个文件中,我喜欢将前5行写入新文件。关闭



该文件,抓住接下来的5行将这些写入新文件......抓住

5行并创建新文件直到处理所有20.000行是

完成。

有没有一种有效的方法在Python中执行此操作?

提前,谢谢你的帮助。

解决方案

9月5日上午11点13分,malibus ... @ gmail.com < malibus ... @ gmail.com>

写道:


我有一个大约20.000行的文本源文件。>从这个文件中,我喜欢将前5行写入新文件。关闭


该文件,抓住接下来的5行将这些文件写入新文件...抓取

5行并创建新文件,直到处理所有20.000行是

完成。

有没有一种有效的方法在Python中执行此操作?

提前,感谢您的帮助。



我会在for循环中使用一个计数器,使用readline方法来重复
遍历20,000行文件。每5行/

迭代重置计数器并关闭文件。要命名具有唯一名称的文件,请使用

时间模块。这样的事情:


x =''filename-%s.txt''%time.time()


玩得开心! />

Mike


ma ******** @ gmail.com escribió:


我有一个大约20.000行的文本源文件。


>>从这个文件中,我喜欢将前5行写入新文件。关闭



该文件,抓住接下来的5行将这些写入新文件......抓住

5行并创建新文件直到处理所有20.000行是完成的。

有没有一种有效的方法在Python中执行此操作?



也许你可以提供一些代码来看看你是如何接近它的?


ky ****** @ gmail.com 写道:


我会用for循环中的计数器使用readline方法来重复
遍历20,000行文件。



文件对象本身就是可迭代的,所以没有必要使用方法来做


< blockquote class =post_quotes>
每5行/次迭代重置计数器并关闭文件。



我会使用一个生成器来获取每个

迭代的五行文件,并直接迭代它而不是文件。


玩得开心!



绝对 - 也自己做功课:)


问候,

Bj ?$

-

BOFH借口#339:


电缆管道中的经理


I have a text source file of about 20.000 lines.

>From this file, I like to write the first 5 lines to a new file. Close

that file, grab the next 5 lines write these to a new file... grabbing
5 lines and creating new files until processing of all 20.000 lines is
done.
Is there an efficient way to do this in Python?
In advance, thanks for your help.

解决方案

On Sep 5, 11:13 am, "malibus...@gmail.com" <malibus...@gmail.com>
wrote:

I have a text source file of about 20.000 lines.>From this file, I like to write the first 5 lines to a new file. Close

that file, grab the next 5 lines write these to a new file... grabbing
5 lines and creating new files until processing of all 20.000 lines is
done.
Is there an efficient way to do this in Python?
In advance, thanks for your help.

I would use a counter in a for loop using the readline method to
iterate over the 20,000 line file. Reset the counter every 5 lines/
iterations and close the file. To name files with unique names, use
the time module. Something like this:

x = ''filename-%s.txt'' % time.time()

Have fun!

Mike


ma********@gmail.com escribió:

I have a text source file of about 20.000 lines.

>>From this file, I like to write the first 5 lines to a new file. Close

that file, grab the next 5 lines write these to a new file... grabbing
5 lines and creating new files until processing of all 20.000 lines is
done.
Is there an efficient way to do this in Python?

Perhaps you could provide some code to see how you approached it?


ky******@gmail.com wrote:

I would use a counter in a for loop using the readline method to
iterate over the 20,000 line file.

file objects are iterables themselves, so there''s no need to do that
by using a method.

Reset the counter every 5 lines/ iterations and close the file.

I''d use a generator that fetches five lines of the file per
iteration and iterate over it instead of the file directly.

Have fun!

Definitely -- and also do your homework yourself :)

Regards,
Bj?rn

--
BOFH excuse #339:

manager in the cable duct


这篇关于文本处理和文件创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆