在文件中查找字符串并获取其行号 [英] Look for a string on a file and get its line number

查看:73
本文介绍了在文件中查找字符串并获取其行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我必须在一个大文件上搜索一个字符串。一旦找到这个字符串

,我就需要得到字符串

在该文件中的行号。你知道如何在

python中做到这一点吗?


谢谢

Hi,

I have to search for a string on a big file. Once this string is
found, I would need to get the number of the line in which the string
is located on the file. Do you know how if this is possible to do in
python ?

Thanks

推荐答案

-On [20080108 09:21],Horacius ReX(ho**********@gmail.com)写道:
-On [20080108 09:21], Horacius ReX (ho**********@gmail.com) wrote:

>我必须在大文件上搜索字符串。找到这个字符串后,我需要得到字符串
在文件中的行号。你知道如何在
python中做到这一点吗?
>I have to search for a string on a big file. Once this string is
found, I would need to get the number of the line in which the string
is located on the file. Do you know how if this is possible to do in
python ?



(假设是ASCII,否则请查看codecs.open()。)


big_file = open(''bigfile。 txt'',''r'')


line_nr = 0

for big_file中的行:

line_nr + = 1

has_match = line.find(''my-string'')

如果has_match 0:

print''在行%d中找到''%(line_nr)


有这样的效果。


-

Jeroen Ruigrok van der Werven < asmodai(-at-)in-nomine.org/ asmodai

??¤??§????????????????????? ?????ˉ??'????? 3 ?????? ?? | ??§?????'??§?? 3
http://www.in-nomine.org/ | http://www.rangaku.org/

如果你认为你知道的很多,你知道的很少...

(Assuming ASCII, otherwise check out codecs.open().)

big_file = open(''bigfile.txt'', ''r'')

line_nr = 0
for line in big_file:
line_nr += 1
has_match = line.find(''my-string'')
if has_match 0:
print ''Found in line %d'' % (line_nr)

Something to this effect.

--
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org/ asmodai
??¤??§????????3 ?????|?????*?????ˉ ??′?????3 ?????? ??|??§?????′??§??3
http://www.in-nomine.org/ | http://www.rangaku.org/
If you think that you know much, you know little...


1月8日晚上7点33分,Jeroen Ruigrok van der Werven< asmo ... @ in in -

nomine.orgwrote:
On Jan 8, 7:33 pm, Jeroen Ruigrok van der Werven <asmo...@in-
nomine.orgwrote:

-On [20080108 09:21],Horacius ReX(horacius .... @ gmail.com )写道:
-On [20080108 09:21], Horacius ReX (horacius....@gmail.com) wrote:

我必须在一个大文件上搜索一个字符串。一旦找到这个字符串

,我就需要得到字符串

在该文件中的行号。你知道如何在

python中做到这一点吗?
I have to search for a string on a big file. Once this string is
found, I would need to get the number of the line in which the string
is located on the file. Do you know how if this is possible to do in
python ?



(假设是ASCII,否则请查看codecs.open()。)

big_file = open(''bigfile。 txt'',''r'')


line_nr = 0

for big_file中的行:

line_nr + = 1

has_match = line.find(''my-string'')

如果has_match 0:


(Assuming ASCII, otherwise check out codecs.open().)

big_file = open(''bigfile.txt'', ''r'')

line_nr = 0
for line in big_file:
line_nr += 1
has_match = line.find(''my-string'')
if has_match 0:



将那个> =


| >>''fubar''。find(''fu'')

| 0

| >>>

Make that >=

| >>''fubar''.find(''fu'')
| 0
| >>>


print''找到行%d''%(line_nr)
print ''Found in line %d'' % (line_nr)


-On [20080108 09:51],John Machin(sj******@lexicon.net)写道:
-On [20080108 09:51], John Machin (sj******@lexicon.net) wrote:

> Make那个> =
>Make that >=



你是对的。对不起,下班后很快就这么做了。 ;)


如果您正在寻找的单词是一个更大的单词的一小部分,我想这个发现也会不那么精确。例如。在一条有门口的行中找到''门''

里面。


所以这不仅仅是为了灵感。 ;)


-

Jeroen Ruigrok van der Werven< asmodai(-at-)in-nomine.org/ asmodai

??¤??§???????????????????????????????????????????????????????? ?? | ??§?????'??§?? 3
http://www.in-nomine.org/ | http://www.rangaku.org/


>从早到晚我都不在视线中/没认出我会变成
>From morning to night I stayed out of sight / Didn''t recognise I''d become



没有比活着我几乎没有生存/总之,超支......

No more than alive I''d barely survive / In a word, overrun...


这篇关于在文件中查找字符串并获取其行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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