Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
faster code for my code
#1
hi, sorry for my bad English,
I have code to remove line(s) that do not start with '[' and end with ']':
    with open('input.raw') as f:
        lines = ""
        for line in f:
            lina = line.strip()
            #print(line)
            if lina.startswith("["):
                if lina.endswith("]"):
                    lines = lines + lina + "\n"
    writing = open('output.raw', 'w')
    writing.write(lines)
    writing.close()
but somehow my file is over 200Mb, and it take hour(s) to get it done,
som how make my code faster?
Reply
#2
The slow part is the accumulative string concatenation at line 8 (the + operator). Try this code
def good_lines(file):
    for line in file:
        line = line.strip()
        if line[:1] == '[' and line[-1] == ']':
            yield line + '\n'

with open('input.raw') as ifh, open('output.raw', 'w') as ofh:
    ofh.writelines(good_lines(ifh))
Pedroski55 and kucingkembar like this post
Reply
#3
(Aug-07-2022, 07:27 AM)Gribouillis Wrote: The slow part is the accumulative string concatenation at line 8 (the + operator). Try this code
def good_lines(file):
    for line in file:
        line = line.strip()
        if line[:1] == '[' and line[-1] == ']':
            yield line + '\n'

with open('input.raw') as ifh, open('output.raw', 'w') as ofh:
    ofh.writelines(good_lines(ifh))
can you belive it, it make hours to under a minute,
thank you, I will give you reputation point
Reply
#4
Is there a reason you're writing this yourself and not using existing tools like, say, grep?
Reply
#5
(Aug-07-2022, 07:51 AM)ndc85430 Wrote: Is there a reason you're writing this yourself and not using existing tools like, say, grep?
what is grep? any link of it?
Reply
#6
(Aug-07-2022, 10:44 AM)kucingkembar Wrote: what is grep? any link of it?

https://www.man7.org/linux/man-pages/man1/grep.1.html
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
(Aug-07-2022, 12:00 PM)rob101 Wrote:
(Aug-07-2022, 10:44 AM)kucingkembar Wrote: what is grep? any link of it?

https://www.man7.org/linux/man-pages/man1/grep.1.html
something error, I tried this:
C:\Users\GIGABYTE>pip install grep
Collecting grep
  Downloading grep-0.3.2.tar.gz (3.2 kB)
  Preparing metadata (setup.py) ... done
Using legacy 'setup.py install' for grep, since package 'wheel' is not installed.
Installing collected packages: grep
  Running setup.py install for grep ... done
Successfully installed grep-0.3.2

C:\Users\GIGABYTE>grep
'grep' is not recognized as an internal or external command,
operable program or batch file.
I will study this
Reply
#8
(Aug-07-2022, 01:07 PM)kucingkembar Wrote: something error, I tried this:

I think that you're confused. grep as I know it, is a command-line utility for searching plain-text data sets for lines that match a regular expression and is a part of Unix type computer operating systems (I can't speak for MS Windows OS, as I don't use it), but so far as I know, there is no Python library of the same name: I stand to be corrected on that, if I'm wrong.

For a regular expression pattern match, I'd use regex:
import re
re.search(<regex>, <string>)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
(Aug-07-2022, 01:17 PM)rob101 Wrote:
(Aug-07-2022, 01:07 PM)kucingkembar Wrote: something error, I tried this:

I think that you're confused. grep as I know it, is a command-line utility for searching plain-text data sets for lines that match a regular expression and is a part of Unix type computer operating systems (I can't speak for MS Windows OS, as I don't use it), but so far as I know, there is no Python library of the same name: I stand to be corrected on that, if I'm wrong.

For a regular expression pattern match, I'd use regex:
import re
re.search(<regex>, <string>)
that linux thing, that why I not use it

anyway is there any regex tutorial with lots of example?
Reply
#10
The rough Windows equivalent of grep is findstr. (CMD shell) or Select-String (PowerShell)

https://docs.microsoft.com/en-us/windows...ds/findstr
https://docs.microsoft.com/en-us/powersh...rshell-7.2
kucingkembar likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,030 Nov-15-2023, 06:56 PM
Last Post: jst
  python multiple try except block in my code -- can we shorten code mg24 10 6,157 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
  Putting code into a function breaks its functionality, though the code is identical! PCesarano 1 1,998 Apr-05-2021, 05:40 PM
Last Post: deanhystad
  HackerRank Problem: Code works on VS Code but not on the HackerRank site Pnerd 3 2,645 Feb-28-2021, 07:12 PM
Last Post: Pnerd
  Converting SQL Code To Python Code Query eddywinch82 13 29,937 Feb-15-2020, 06:42 PM
Last Post: buran
  how can I improve the code to get it faster? aquerci 2 1,720 Feb-15-2020, 02:52 PM
Last Post: aquerci
  code not writing to projNameVal portion of code. umkc1 1 1,680 Feb-05-2020, 10:05 PM
Last Post: Larz60+
  Simple code works in Jupyter but not VS Code Matt_O 2 3,938 Nov-17-2019, 01:15 AM
Last Post: Matt_O
  How does the code run? My code wrong? jollydragon 0 1,744 Oct-10-2019, 06:24 AM
Last Post: jollydragon
  Can someone please help me convert this simple C ROT cipher code to Python code? boohoo9 5 3,457 Jun-14-2019, 03:02 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020