Home > Mobile >  How to read this txt and get all the lines with 'take'?
How to read this txt and get all the lines with 'take'?

Time:05-01

I want to get all the lines in the arcane script that has 'take', but I cannot do it with my code, can you show me how to do it? thanks!!!

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)

Sorry, I don't know how to upload my txt here, so i can just show part of the script here. It is line by line in my txt file.

part of the script:

♪ 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?

expected output:

♪ I'll take what you can spare ♪
Hey, Powder. Come take a look.

CodePudding user response:

Try:

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)

CodePudding user response:

  • Assuming that's how your file is originally formatted ('\n' present at the end of each line):

file_img

  • Then you can use 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: results_img

    • ♪ I'll take what you can spare
    • ♪ Hey, Powder. Come take a look.
  • Related