How to write to a text file the output of for loop?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

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

df.close()

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

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Convolutional RCCn's for extracting the text out of images
CRNNs combine both convolutional and recurrent architectures and is widely used in text detection and optical character recognition (OCR). In this project, we are going to use a CRNN architecture to detect text in sample images. The data we are going to use is TRSynth100k from Kaggle. Given an image containing some text, the goal here is to correctly identify the text using the CRNN architecture. We are going to train the model end-to-end from scratch.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Time Series LSTM forecasting
In this project, we will use time-series forecasting to predict the values of a sensor using multiple dependent variables. A variety of machine learning models are applied in this task of time series forecasting. We will see a comparison between the LSTM, ARIMA and Regression models. Classical forecasting methods like ARIMA are still popular and powerful but they lack the overall generalizability that memory-based models like LSTM offer. Every model has its own advantages and disadvantages and that will be discussed. The main objective of this article is to lead you through building a working LSTM model and it's different variants such as Vanilla, Stacked, Bidirectional, etc. There will be special focus on customized data preparation for LSTM.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

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.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.