如何阅读此 txt 并获取所有带有 'take' 的行?
How to read this txt and get all the lines with 'take'?
我想获取包含 'take' 的奥术脚本中的所有行,但我无法用我的代码做到这一点,你能告诉我怎么做吗?谢谢!!!
x = open("Arcane.S01E01.Welcome.to.the.Playground.1080p.NF.WEB-DL.DDP5.1.x264-TEPES.英文.txt")
for line in x:
if not 'take' in line:
continue
print(line)
抱歉,我不知道如何在这里上传我的txt,所以我只能在这里展示部分脚本。它在我的 txt 文件中逐行显示。
部分脚本:
♪ Dear friend across the river ♪
♪ My hands are cold and bare ♪
♪ Dear friend across the river ♪
♪ I'll take what you can spare ♪
♪ I ask of you a penny ♪
♪ My fortune it will be ♪
♪ I ask you without envy ♪
♪ We raise no mighty towers ♪
♪ Our homes are built of stone ♪
♪ So come across the river ♪
We're almost there.
Aw, man.
Hey, Powder. Come take a look.
Whoa. It's nice getting above it all, huh?
One day, I'm gonna ride in one of those things.
And one day, I'm gonna shoot one of 'em down.
Vi, are, are you sure about this?
-Look, if we get caught, we're-- -We're not gonna get caught.
We'll be in and out before anyone notices.
All right, everybody, follow me. Just don't look down.
Couldn't we have at least just walked there?
Gotta stay out of sight for this one.
Called it. This is on you, Vi.
-I'll get her. -No.
Powder, look at me.
What did I tell you? That I'm ready.
That's right! So?
Thanks.
What if Vander finds out we're all the way up here?
Look around you. You think anyone topside's going hungry?
Besides, this is exactly the sort of job Vander would've pulled
when he was our age.
I'm going. Are you with me or not?
Vander's gonna kill us. Yeah, only if we screw up.
So don't screw up.
All clear.
Who locks their balcony?
预期输出:
♪ I'll take what you can spare ♪
Hey, Powder. Come take a look.
尝试:
with open("Arcane.S01E01.Welcome.to.the.Playground.1080p.NF.WEB-DL.DDP5.1.x264-TEPES.英文.txt", "r") as a_file:
for line in a_file:
stripped_line = line.strip().decode("utf-16")
if 'take' in stripped_line :
print(stripped_line)
- 假设您的文件最初是这样格式化的('\n' 出现在每一行的末尾):
file_img
然后就可以使用splitlines()
# Reading file and decoding it because of '♪'
file_path_str = "Arcane.S01E01.Welcome.to.the.Playground.1080p.NF.WEB-DL.DDP5.1.x264-TEPES.英文.txt"
with open(file_path_str, "rb") as txt_file_obj:
x_str = txt_file_obj.read().decode("UTF-8")
# Getting the lines where 'take' is present
for line_str in x_str.splitlines():
if 'take' in line_str:
print(line_str)
结果:
results_img
- ♪我会带走你有空的
- ♪ 嘿,粉。快来看看
我想获取包含 'take' 的奥术脚本中的所有行,但我无法用我的代码做到这一点,你能告诉我怎么做吗?谢谢!!!
x = open("Arcane.S01E01.Welcome.to.the.Playground.1080p.NF.WEB-DL.DDP5.1.x264-TEPES.英文.txt")
for line in x:
if not 'take' in line:
continue
print(line)
抱歉,我不知道如何在这里上传我的txt,所以我只能在这里展示部分脚本。它在我的 txt 文件中逐行显示。
部分脚本:
♪ Dear friend across the river ♪
♪ My hands are cold and bare ♪
♪ Dear friend across the river ♪
♪ I'll take what you can spare ♪
♪ I ask of you a penny ♪
♪ My fortune it will be ♪
♪ I ask you without envy ♪
♪ We raise no mighty towers ♪
♪ Our homes are built of stone ♪
♪ So come across the river ♪
We're almost there.
Aw, man.
Hey, Powder. Come take a look.
Whoa. It's nice getting above it all, huh?
One day, I'm gonna ride in one of those things.
And one day, I'm gonna shoot one of 'em down.
Vi, are, are you sure about this?
-Look, if we get caught, we're-- -We're not gonna get caught.
We'll be in and out before anyone notices.
All right, everybody, follow me. Just don't look down.
Couldn't we have at least just walked there?
Gotta stay out of sight for this one.
Called it. This is on you, Vi.
-I'll get her. -No.
Powder, look at me.
What did I tell you? That I'm ready.
That's right! So?
Thanks.
What if Vander finds out we're all the way up here?
Look around you. You think anyone topside's going hungry?
Besides, this is exactly the sort of job Vander would've pulled
when he was our age.
I'm going. Are you with me or not?
Vander's gonna kill us. Yeah, only if we screw up.
So don't screw up.
All clear.
Who locks their balcony?
预期输出:
♪ I'll take what you can spare ♪
Hey, Powder. Come take a look.
尝试:
with open("Arcane.S01E01.Welcome.to.the.Playground.1080p.NF.WEB-DL.DDP5.1.x264-TEPES.英文.txt", "r") as a_file:
for line in a_file:
stripped_line = line.strip().decode("utf-16")
if 'take' in stripped_line :
print(stripped_line)
- 假设您的文件最初是这样格式化的('\n' 出现在每一行的末尾):
file_img
然后就可以使用splitlines()
# Reading file and decoding it because of '♪' file_path_str = "Arcane.S01E01.Welcome.to.the.Playground.1080p.NF.WEB-DL.DDP5.1.x264-TEPES.英文.txt" with open(file_path_str, "rb") as txt_file_obj: x_str = txt_file_obj.read().decode("UTF-8") # Getting the lines where 'take' is present for line_str in x_str.splitlines(): if 'take' in line_str: print(line_str)
结果: results_img
- ♪我会带走你有空的
- ♪ 嘿,粉。快来看看