• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / Python / Using readlines() and strip() to Remove Spaces and \n from File in Python

Using readlines() and strip() to Remove Spaces and \n from File in Python

June 20, 2022 Leave a Comment

When reading the contents of a file, whitespace sometimes can cause us troubles. To remove whitespace from each line when using Python, you can use the Python strip() function.

myfile = open("example.txt", "r")

lines = myfile.readlines()

for line in lines:
    stripped_line = line.strip()

When working with files, if you have bad inputs, you can have some headaches. One such situation is if you have unwanted characters or whitespace in your files.

To get rid of whitespace, you can use the Python string strip() function.

strip() removes leading and trailing characters from a string. The strip() function will remove newline characters as well as regular spaces.

Below is an example of how you can read all lines with readlines() and use strip() to remove whitespace from the lines.

myfile = open("example.txt", "r")

lines = myfile.readlines()

for line in lines:
    stripped_line = line.strip()

Hopefully this article has been useful for you to learn how to remove whitespace from the lines of a file using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Check If a Number is a Perfect Square
  • 2.  Apply Function to All Elements in List in Python
  • 3.  Intersection of Two Lists in Python
  • 4.  pandas to_pickle – Write DataFrame to Pickle File
  • 5.  Using Python Selenium Webdriver to Refresh Page
  • 6.  How to Filter pandas DataFrame by Date
  • 7.  Get Week Number from Date in Python
  • 8.  List of Turtle Shapes in Python
  • 9.  Python Square Root Without Math Module – ** or Newton’s Method
  • 10.  How to Check if a Letter is in a String Using Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy