Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8601581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:52:37+00:00 2026-06-12T01:52:37+00:00

I have a text file in this format: abc? cdfde nhj.cde’ dfwe-df$sde….. How can

  • 0

I have a text file in this format:

abc? cdfde" nhj.cde' dfwe-df$sde.....  

How can i ignore all the special characters, blanks, numbers, end of the lines, etc and write only the characters in another file?For example, the above file becomes

abccdfdenhjcdedfwedfsde.....  

And from this output file,

  • Should able to read single character by character till the end of file.
  • Should be able to read two characters at a time, like ab,bc,cc,cd,df,… from above file
  • Should be able to read three characters at a time, like abc,bcc,ccd,cdf,… from the above file

First of all, how can i read only characters and write to external file?
I can read single character by character by using f.read(1) till end of file.How can i apply this to read 2,3 chars at a time, that too skipping only one character(that is, if i have abcd, i should read ab,bc,cd but not ab,cd(this, i think can be done by f.read(2))). Thanks. I am doing this for cryptanalysis work to analyze ciphertexts by frequency.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T01:52:39+00:00Added an answer on June 12, 2026 at 1:52 am

    If you need to peek ahead (read a few extra characters at a time), you need a buffered file object. The following class does just that:

    import io
    
    class AlphaPeekReader(io.BufferedReader):
        def readalpha(self, count):
            "Read one character, and peek ahead (count - 1) *extra* characters"
            val = [self.read1(1)]
    
            # Find first alpha character
            while not val[0].isalpha():
                if val == ['']:
                    return ''  # EOF
                val = [self.read1(1)]
    
    
            require = count - len(val)
            peek = self.peek(require * 3)  # Account for a lot of garbage
            if peek == '':  # EOF
                   return val[0]
    
            for c in peek:
                if c.isalpha():
                   require -= 1
                   val.append(c)
                   if not require:
                       break
    
            # There is a chance here that there were not 'require' alpha chars in peek
            # Return anyway.
            return ''.join(val)
    

    This attempts to find extra characters beyond the one character you are reading, but doesn’t make a guarantee it’ll be able to satisfy your requirements. It could read fewer if we are at the end of the file or if there is a lot of non-alphabetic text in the next block.

    Usage:

    with AlphaPeekReader(io.open(filename, 'rb')) as alphafile:
        alphafile.readalpha(3)
    

    Demo, using a file with your example input:

    >>> f = io.open('/tmp/test.txt', 'rb')
    >>> alphafile = AlphaPeekReader(f)
    >>> alphafile.readalpha(3)
    'abc'
    >>> alphafile.readalpha(3)
    'bcc'
    >>> alphafile.readalpha(3)
    'ccd'
    >>> alphafile.readalpha(10)
    'cdfdenhjcd'
    >>> alphafile.readalpha(10)
    'dfdenhjcde'
    

    To use the readalpha() calls in a loop, where you get each and every character separately plus the two next 2 bytes, use the iter() with a sentinel:

    for alpha_with_extra in iter(lambda: alphafile.readalpha(3), ''):
        # Do something with alpha_with_extra
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i have my text file in this format **4 1250000209852 01 XXXX XXXX
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I have a text file, it has the data format like below: (1796208919349287,2592294224942165,1527446512828944,'abc','<a href=\'/users/7310739222965755\'>@xxd</a>
I have a text file which contain rows in this format: com1,System Access,MinimumPasswordAge,1 com1,System
I have this text file that have lines made in a certain format just
I have a large file in this format: >Abc1 | SOME TEXT atgcgnntcagacagacaa >Abc2
I have a text file of this format: L O A D C A
I have text file format like this SKI SKII SKIII SKIV SKV SKVI SKVII
I have text file like this format: ... SomeText.any_text/ch SomeText2.any_3/ch 5.6e-5 SomeText.any_text/ch something.else.point.separated/ch4 5.4e5
I have a text file like this: A B C And each element has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.