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

  • Home
  • SEARCH
  • 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 8990009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:24:08+00:00 2026-06-15T22:24:08+00:00

How can you open a .txt file in Python and get the exact string

  • 0

How can you open a .txt file in Python and get the exact string as it is in the file?

I have a text file containing regular expression e.g.:

\\*(.*?)\\n

When I open the file in Python:

open('regEx.txt', 'r')

I’m getting:

\\\\*(.*?)\\\\n

Is there a way to open this file and get the string exactly as it is written in the file?

  • 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-15T22:24:09+00:00Added an answer on June 15, 2026 at 10:24 pm

    You are slightly mixing up a few string representations here. The actual regular expression (disregarding any language specific oddities) would simply be

    \*(.*?)\n
    

    (literally those 9 characters)

    However, I suppose you’ve either been using Java or Python without raw strings. In that case, to create the above string in memory your code has to double the backslashes:

    "\\*(.*?)\\n"
    

    This is because, if you didn’t double them, Python would already remove them when compiling the string. But now the string is compiled to these 9 characters again: \*(.*?)\n. If print these out you will get (as jd. said) a display including the double backslashes. But if you call len(string) it will say 9, not 11.

    So you only want 9 characters. Then why write 11 in your file? If you write eleven, then upon display the backslashes will be double escaped again. But call len(input) on the result of open. It will say 11, not 15.

    This is also why you should always use raw strings when defining regular expressions within your code. Then you never need any additional escapes (except for quotes):

    r"\*(.*?)\n"
    

    which will again leave you with 9 characters (because the backslashes are left untouched upon compilation of the string).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file containing python's object as string, then i open it and
Exact error: $ ./script.pl file.txt Can't open file.txt: No such file or directory at
I have following script in Python 3.2.3: try: file = open('file.txt', 'r') except IOError:
The code chokes at fopen(): <?php ini_set('display_errors',1); error_reporting(E_ALL); $fp = fopen(/path/to/file/some_file.txt,a) or die(can't open
How this file can be closed. Any idea? with open('output.txt','w', encoding='UTF-8') as output: output.writelines(str(i)+'\n'
In recent versions of python, one can use something like with open('abc.txt') as f:
I want to write something in a file. for example, fo=open('C:\\Python\\readline_test.txt','a') for i in
Let's say I have a text file containing: Dan Warrior 500 1 0 Is
My question is related to file-input in Python, using open() . I have a
For the following Python code: fp = open('output.txt', 'wb') # Very big file, writes

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.