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 8501783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:18:14+00:00 2026-06-11T01:18:14+00:00

In Python, calling e.g. temp = open(filename,’r’).readlines() results in a list in which each

  • 0

In Python, calling e.g. temp = open(filename,'r').readlines() results in a list in which each element is a line from the file. However, these strings have a newline character at the end, which I don’t want.

How can I get the data without the newlines?

  • 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-11T01:18:15+00:00Added an answer on June 11, 2026 at 1:18 am

    You can read the whole file and split lines using str.splitlines:

    temp = file.read().splitlines()
    

    Or you can strip the newline by hand:

    temp = [line[:-1] for line in file]
    

    Note: this last solution only works if the file ends with a newline, otherwise the last line will lose a character.

    This assumption is true in most cases (especially for files created by text editors, which often do add an ending newline anyway).

    If you want to avoid this you can add a newline at the end of file:

    with open(the_file, 'r+') as f:
        f.seek(-1, 2)  # go at the end of the file
        if f.read(1) != '\n':
            # add missing newline if not already present
            f.write('\n')
            f.flush()
            f.seek(0)
        lines = [line[:-1] for line in f]
    

    Or a simpler alternative is to strip the newline instead:

    [line.rstrip('\n') for line in file]
    

    Or even, although pretty unreadable:

    [line[:-(line[-1] == '\n') or len(line)+1] for line in file]
    

    Which exploits the fact that the return value of or isn’t a boolean, but the object that was evaluated true or false.


    The readlines method is actually equivalent to:

    def readlines(self):
        lines = []
        for line in iter(self.readline, ''):
            lines.append(line)
        return lines
    
    # or equivalently
    
    def readlines(self):
        lines = []
        while True:
            line = self.readline()
            if not line:
                break
            lines.append(line)
        return lines
    

    Since readline() keeps the newline also readlines() keeps it.

    Note: for symmetry to readlines() the writelines() method does not add ending newlines, so f2.writelines(f.readlines()) produces an exact copy of f in f2.

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

Sidebar

Related Questions

Possible Duplicate: Calling Python from Objective-C I'm a long-time Python programmer and short-time Cocoa
Possible Duplicate: Calling Python from JavaScript I have a test.py and test.js . I
I'm calling a java program from my python code in the following way: subprocess.check_output([java,
Both Ruby and Python have the ability of calling the debugger from code (
I am calling a python script from PHP. The python program has to return
I am calling a python script with the following command line: myscript.py --myopt=[(5.,5.),(-5.,-5.)] The
I'm calling pipe.communicate from Python's subprocess module from Python 2.6. I get the following
I'm calling a python script (B) from another python script (A). Using subprocess.call, how
I am calling python functions from C++. I was wondering if it is possible
I am currently calling a C# script file ( http://www.csscript.net ) from a C#

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.