close

How save a array to text file in python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How save a array to text file in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How save a array to text file in python?

  1. How save a array to text file in python?

    Probably the simplest method is to use the json module, and convert the array to list in one step:

  2. save a array to text file in python

    Probably the simplest method is to use the json module, and convert the array to list in one step:

Method 1

This is a possible solution:

data = [['nameservers','panel'], ['nameservers','panel']]

with open("output.txt", "w") as txt_file:
    for line in data:
        txt_file.write(" ".join(line) + "\n") # works with any number of elements in a line

Method 2

Probably the simplest method is to use the json module, and convert the array to list in one step:

import json

with open('output.txt', 'w') as filehandle:
json.dump(array.toList(), filehandle)

Using the json format allows interoperability between many different systems.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read