Python 如何查找文件中的特定行?
我有一个文本文件,如下所示:Python 如何查找文件中的特定行?,python,file,seek,Python,File,Seek,我有一个文本文件,如下所示: 1 /run/media/dsankhla/Entertainment/English songs/Apologise (Feat. One Republic).mp3 3 /run/media/dsankhla/Entertainment/English songs/Bad Meets Evil.mp3 5 /run/media/dsankhla/Entertainment/English songs/Love Me Like You DO.mp3 我想在文件中
1
/run/media/dsankhla/Entertainment/English songs/Apologise (Feat. One Republic).mp3
3
/run/media/dsankhla/Entertainment/English songs/Bad Meets Evil.mp3
5
/run/media/dsankhla/Entertainment/English songs/Love Me Like You DO.mp3
我想在文件中搜索一个特定的行,假设该行是
song_path=/run/media/dsankla/Entertainment/English songs/Bad Meets Evil.mp3
然后我想寻找后面的lensong_路径+2,这样我可以指向文件中的3。我该怎么做?
这是我目前的代码:
txt = open(".songslist.txt", "r+")
if song_path in txt.read():
byte = len(song_path)
txt.seek(-(byte), 1)
freq = int(txt.readline())
print freq # 3
freq = freq + 1
txt.seek(-2,1)
txt.write(str(freq))
txt.close()
最好的方法是使用seek,如本例所示:
fp = open('myfile')
last_pos = fp.tell()
line = fp.readline()
while line != '':
if line == 'SPECIAL':
fp.seek(last_pos)
change_line()#whatever you must to change
break
last_pos = fp.tell()
line = fp.readline()
必须使用fp.tell将位置值指定给变量。然后使用fp.seek您可以返回。最好的方法是使用seek,如本例所示:
fp = open('myfile')
last_pos = fp.tell()
line = fp.readline()
while line != '':
if line == 'SPECIAL':
fp.seek(last_pos)
change_line()#whatever you must to change
break
last_pos = fp.tell()
line = fp.readline()
必须使用fp.tell将位置值指定给变量。然后使用fp.seek,您可以返回。如果您的文件不是太大,无法放入内存,读/写速度非常慢,您可以绕过任何低级操作,如seek,只需完全读取文件,更改您想要更改的内容,然后将所有内容写回
# read everything in
with open(".songslist.txt", "r") as f:
txt = f.readlines()
# modify
path_i = None
for i, line in enumerate(txt):
if song_path in line:
path_i = i
break
if path_i is not None:
txt[path_i] += 1 # or what ever you want to do
# write back
with open(".songslist.txt", "w") as f:
f.writelines(txt)
使用seek时,如果不写入字节perfekt,则需要小心,即:
f = open("test", "r+")
f.write("hello world!\n12345")
f.seek(6) # jump to the beginning of "world"
f.write("1234567") # try to overwrite "world!" with "1234567"
# (note that the second is 1 larger then "world!")
f.seek(0)
f.read() # output is now "hello 123456712345" note the missing newline
如果您的文件不是太大,无法放入内存,读/写速度非常慢,那么您可以绕过任何低级操作,如查找,完全读取文件,更改您想要更改的内容,然后将所有内容写回
# read everything in
with open(".songslist.txt", "r") as f:
txt = f.readlines()
# modify
path_i = None
for i, line in enumerate(txt):
if song_path in line:
path_i = i
break
if path_i is not None:
txt[path_i] += 1 # or what ever you want to do
# write back
with open(".songslist.txt", "w") as f:
f.writelines(txt)
使用seek时,如果不写入字节perfekt,则需要小心,即:
f = open("test", "r+")
f.write("hello world!\n12345")
f.seek(6) # jump to the beginning of "world"
f.write("1234567") # try to overwrite "world!" with "1234567"
# (note that the second is 1 larger then "world!")
f.seek(0)
f.read() # output is now "hello 123456712345" note the missing newline
如果您的文件不是太大,并且您可以在内存中完全读取它,您可以使用readlines,只需查看n+1行。@syntonym代码的答案将是helpful@syntonym甚至我也需要更改文件中的那一行。如果你的文件不是太大,并且你可以在内存中完整地读取它,你可以使用readlines并简单地查看这一行n+1。@syntonym带有代码的答案将是helpful@syntonym甚至我也需要更改文件中的那一行。