[求助]python读取文本提取指字内容

Python/PHP/Perl 开发与设计
回复
djhui
帖子: 85
注册时间: 2007-03-10 12:34
送出感谢: 0
接收感谢: 0
联系:

[求助]python读取文本提取指字内容

#1

帖子 djhui » 2008-10-09 15:13

我有一个文本,有很多内容,部分如下,但我只须要Filename:************ 这此行,用什么办法能提取出来



Filename: edgy/acpid_1.0.6-9ubuntu4_i386.deb
Size: 32638
MD5sum: abc021aa98ed9d4ed606048a5f1f8f31
Section: admin
Priority: optional
Homepage: http://acpid.sourceforge.net/
Description: Utilities for using ACPI power management
Modern computers support the Advanced Configuration and Power Interface (ACPI)
to allow intelligent power management on your system and to query battery and
configuration status.
.
ACPID is a completely flexible, totally extensible daemon for delivering
ACPI events. It listens on a file (/proc/acpi/event) and when an event
occurs, executes programs to handle the event. The programs it executes
are configured through a set of configuration files, which can be
dropped into place by packages or by the admin.

Package: alacarte
Version: 0.11.6-0ubuntu1
Architecture: all
Maintainer: Ubuntu Desktop Team <ubuntu-desktop@lists.ubuntu.com>
Installed-Size: 1208
Depends: python (>= 2.4), python-central (>= 0.6.7), python-gtk2 (>= 2.8.0), python-glade2 (>= 2.8.0), python-gmenu (>= 2.15.4.1), gnome-menus (>= 2.15.4.1), python-gnome2 (>= 2.6)
Conflicts: smeg (<< 0.8-0ubuntu1)
Replaces: smeg
Provides: smeg
Filename: edgy/alacarte_0.11.6-0ubuntu1_all.deb
Size: 60548
MD5sum: 5e8f7235a207cff373b84960f39a4b96
Section: utils
Priority: optional
Description: easy GNOME menu editing tool
Alacarte is an easy-to-use menu editor for GNOME that can add
and edit new entries and menus. It works with the freedesktop.org
menu specification and should work with any desktop environment
that uses the spec.

Package: alsa-base
Source: alsa-driver
Version: 1.0.17.dfsg-1ubuntu1
Architecture: all
Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
Installed-Size: 408
Depends: lsof (>= 4.64), module-init-tools (>= 3.2.1), linux-sound-base
Recommends: alsa-utils
Suggests: apmd (>= 3.0.2-1), alsa-oss
Conflicts: alsa-utils (<< 1.0.9a-4), discover (<< 2.0.7-1), discover1 (<< 1.7.3), lsof-2.2 (<< 4.64), modutils (= 2.3.20-1)
Provides: alsa
Filename: edgy/alsa-base_1.0.17.dfsg-1ubuntu1_all.deb
Size: 219472
头像
xiooli
帖子: 6956
注册时间: 2007-11-19 21:51
来自: 成都
送出感谢: 0
接收感谢: 3 次
联系:

Re: [求助]python读取文本提取指字内容

#2

帖子 xiooli » 2008-10-09 15:50

grep "Filename" file

btw:只要python的么?
ygf
帖子: 458
注册时间: 2006-11-27 12:25
送出感谢: 0
接收感谢: 0

Re: [求助]python读取文本提取指字内容

#3

帖子 ygf » 2008-10-13 20:19

我也是初学,大家探讨一下。

假设你的文件名是test.txt

f = open('test.txt')
g=[i.strip() for i in f if i.startswith('Filename:')]
f.close()

g是以’Filename:'开头的行的列表。
djhui
帖子: 85
注册时间: 2007-03-10 12:34
送出感谢: 0
接收感谢: 0
联系:

Re: [求助]python读取文本提取指字内容

#4

帖子 djhui » 2008-10-14 10:35

你好,g所读出来的没有换行,怎么办
ygf
帖子: 458
注册时间: 2006-11-27 12:25
送出感谢: 0
接收感谢: 0

Re: [求助]python读取文本提取指字内容

#5

帖子 ygf » 2008-10-14 10:44

如果想保留换行符,不用strip()方法就行了。
g=[i for i in f if i.startswith('Filename:')]
回复

回到 “Python/Php/Perl”