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

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

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.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.