How to write to a text file the output of for loop?

How to write to a text file the output of for loop?

How to write to a text file the output of for loop?

This recipe helps you write to a text file the output of for loop


Recipe Objective

While working with python we might need to save our output in a text file. Generally they come from loops which sometimes might be tricky to handle.

So this recipe is a short example on how to add for loop output to a text file. Let's get started.

Step 1 - Opening a text file

df=open('text file','w')

To start, we have to first create an object of a text file using the open function. We have used 'w' which refers to write function. Now df object stores the text file and can be easily written upon.

Step 2 - Adding a test line in the file

df.write('We will be seeing an interated printing of numbers between 0 to 10\n')

We have added a test line using write function.

Step 3 - Writing a for loop over the file

for i in range(0,11): df.write(str(i)) df.write('\n')

We have created a loop, iterating over i from 0 to 10. Write function only accepts string values, hence converted the same using str function and finally adding a new line using '\n'

Step 4 - Closing the text file


After all the amendments are done, use close fuction to simply close the file created.

Step 5 - Let's look at our dataset now

Once we run the above code snippet, we will not be seeing any output. However, a file named test file would have been created in the directory you are working on, containing all your inputs.

Relevant Projects

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.