skip space all at once while reading from a file

Shubham Kumar

I am doing a project (python language) which involves using OCR (using tesseract-ocr) to get text from an image and store it into file. Then I have to read the file character by character and perform some functions for the detected characters. The problem I encountered is that sometimes the file created after conversion has a lot of blank spaces (even blank lines) at the beginning of the text file. I do not have to use any function for the spaces so i want to ignore them all at once so that it could save my time. I am running the code on a raspberry-pi which has very less memory and it takes some time to compare each character and skip one by one.

camera.capture('test.png')

camera.resolution = (1920, 1080)
camera.brightness = 60 

call(["tesseract","/home/pi/Desktop/fyp_try/test.png","/home/pi/Desktop/fyp_try/totext"])

f = open('/home/pi/Desktop/fyp_try/totext.txt','r')

message = f.read()
print(message)

for i in message:
    print(i)
    if(i>='a')and(i<='z'):
        lst=a[i]
        lstoperate()
    elif(i>='A')and(i<='Z'):
        lst=a['dot']
        stoperate()
       time.sleep(2)
        smol=i.lower()
        lst=a[smol]
        lstoperate()
    elif (i>='0')and(i<='9'):
        lst=a['numsign']
        lstoperate()
        print(ord(i))
  ..............

operation on each character is followed by a sleep time of 2-3 seconds. this also happens when spaces are encountered. Is there any way i can ignore all the spaces at once till the beginning of a non space character in the file while reading it.

ShadowRanger

If you want to strip all the whitespace in a single operation with low resource costs, you'll want to avoid split/join (which works, but has a high temporary memory cost).

There are two obvious approaches, the lazy filtering approach:

 from itertools import filterfalse

 ...

 for i in filterfalse(str.isspace, message):
     ...

which never makes a new str, but simply filters out the characters you don't care about as you go.

Or to strip them all up front (doubling initial memory consumption, but then dropping to just what the stripped version requires), use str.translate:

 from string import whitespace

 dropspaces = str.maketrans('', '', whitespace)

 ...

 message = f.read().translate(dropspaces)

That will strip all ASCII whitespace as if doing .replace(' ', '').replace('\n', '').replace('\r', '').etc..., but in a single pass, producing a single output string with all the whitespace stripped at once.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

(Java) How can I skip a space when reading from a file?

How to skip over white spaces in a string while reading from a file

zcat skip streaming first line while reading from a compressed file

How to skip primitive data values while reading from file

How skip reading " : " while taking input from a file in C++

Reading a file into a vector all at once

how do i skip part of each line while reading from file in python

How to skip carriage returns in csv file while reading from cloud storage using google cloud dataflow in java

No space between words while reading and extracting the text from a pdf file in python?

Skip certain number of lines while reading a text file - BufferedReader Java

Skip first couple of lines while reading lines in Python file

Skip first couple of lines while reading lines in Python file

Skip blank lines while reading .csv file using opencsv (java)

How to skip lines while reading a CSV file as a dataFrame using PySpark?

skip header while reading a CSV file in Apache Beam

skip second row of dataframe while reading csv file in python

Skip header of CSV file while reading in core Java with below approach

Golang: How to skip struct fields while reading from a buffer?

Why in outer loop isstringstream iterator is iterating only once ,while reading from file , though there are other lines exist in that file?

Issue while reading a file from WAR file

Reading an inputStream all at once

Is there a way to force all the columns to be strings while reading data from a .csv file into a DataTable?

How to skip multiple lines when reading from a file pointer in PHP

How to read while reading from a file in bash

Weird loop while reading from a file in C

Infinity loop while reading data from file

Problem while reading from the stem file in C

Strange behaviour while reading file from Java

Strange chars while reading from file with fgetc

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  3. 3

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  4. 4

    pump.io port in URL

  5. 5

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    Do Idle Snowflake Connections Use Cloud Services Credits?

  9. 9

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  10. 10

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  11. 11

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  12. 12

    Generate random UUIDv4 with Elm

  13. 13

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  14. 14

    Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

  15. 15

    flutter: dropdown item programmatically unselect problem

  16. 16

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  17. 17

    EXCEL: Find sum of values in one column with criteria from other column

  18. 18

    Pandas - check if dataframe has negative value in any column

  19. 19

    How to use merge windows unallocated space into Ubuntu using GParted?

  20. 20

    Make a B+ Tree concurrent thread safe

  21. 21

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

HotTag

Archive