2007年07月16日 星期一 10:58
比如,一个很大的文本文件,我只想更改其中一部分的内容(如某一行的内容或第p至 第q字节的内容),除了把它全部读到内存中来、更改、再写回去这种方法外,还有效 率更高一些的办法吗? 嘿嘿,似乎是很菜的问题... :-)
2007年07月16日 星期一 11:02
ÕýÔòÃ²ËÆÐ§ÂʸüµÍ£¬²ËÄñmark ÔÚ07-7-16£¬Allentro <allentro在gmail.com> дµÀ£º > > ±ÈÈ磬һ¸öºÜ´óµÄÎı¾Îļþ£¬ÎÒÖ»Ïë¸ü¸ÄÆäÖÐÒ»²¿·ÖµÄÄÚÈÝ£¨ÈçijһÐеÄÄÚÈÝ»òµÚpÖÁ > µÚq×Ö½ÚµÄÄÚÈÝ£©£¬³ýÁ˰ÑËüÈ«²¿¶Áµ½ÄÚ´æÖÐÀ´¡¢¸ü¸Ä¡¢ÔÙд»ØÈ¥ÕâÖÖ·½·¨Í⣬»¹ÓÐЧ > Âʸü¸ßһЩµÄ°ì·¨Â𣿠> > ºÙºÙ£¬ËƺõÊǺܲ˵ÄÎÊÌâ... :-) > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070716/0aa6e1ea/attachment.htm
2007年07月16日 星期一 11:08
On 7/16/07, Allentro <allentro在gmail.com> wrote: > 比如,一个很大的文本文件,我只想更改其中一部分的内容(如某一行的内容或第p至 > 第q字节的内容),除了把它全部读到内存中来、更改、再写回去这种方法外,还有效 > 率更高一些的办法吗? > > 嘿嘿,似乎是很菜的问题... :-) 因为文件不是数据库,而是随机存储的,如果是替换,不改变文件大小,可以考虑定位修改的方式,如果大小改变,恐怕只能如此了。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2007年07月16日 星期一 11:17
WARNING: 这不是正解。请楼下达人补充真正有效的方法, 比如 Pickle、Sqlite 一类的。
Python 2.5.1 Stackless 3.1b3 060516 (release25-maint, May 23 2007, 12:31:42)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> open('demo', 'w').write('0123456789')
>>> fd = open('demo', 'r+')
>>> fd.seek(5)
>>> fd.write('xxxxx')
>>> fd.close()
>>> print open('demo').read()
01234xxxxx
在 07-7-16,徐鹏<beck917在gmail.com> 写道:
> 正则貌似效率更低,菜鸟mark
>
> 在07-7-16,Allentro <allentro在gmail.com> 写道:
> > 比如,一个很大的文本文件,我只想更改其中一部分的内容(如某一行的内容或第p至
> > 第q字节的内容),除了把它全部读到内存中来、更改、再写回去这种方法外,还有效
> > 率更高一些的办法吗?
> >
> > 嘿嘿,似乎是很菜的问题... :-)
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
---
沈崴 (努力学习 Python 中 ...)
http://blog.163.com/eishn
Zeuux © 2025
京ICP备05028076号