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

The Programming Expert

Solving All of Your Programming Headaches

  • 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.  How to Repeat a Function in Python
  • 2.  Using Selenium to Check if Element Exists in Python
  • 3.  pandas median – Find Median of Series or Columns in DataFrame
  • 4.  Date Format YYYYMMDD in Python
  • 5.  Remove None From List Using Python
  • 6.  Using Python to Print Variable Type
  • 7.  Python Indicator Function – Apply Indicator Function to List of Numbers
  • 8.  How to Check if Number is Power of 2 in Python
  • 9.  Pythagorean Theorem in Python – Calculating Length of Triangle Sides
  • 10.  Get pandas Column Names as List in 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