Python write list to file

Python is a simple syntax language that helps to get your choice output with the help of few lines of code. There is no need to declare header files etc.  Python allows writing files within …

Python is a simple syntax language that helps to get your choice output with the help of few lines of code. There is no need to declare header files etc. 

Python allows writing files within simple steps.  In this article, Python creates and writes data inside the file of a given extension such as .txt, .csv,.json, etc. Here we are discussing how to create the text file, JSON file, or CSV file, open them into writing mode and write lists data into different formats as mentioned below.

    We perform the below given seven methods to write a list into different formats of file.

  1. Python write list to file.
  2. Python write list to file with newlines.
  3. Python write list of numbers to file.
  4. Python write list to file with brackets.
  5. Python write list to file without brackets.
  6. Python write list to file as JSON.
  7. Python write list to CSV.

Here we will use the open() method to create and open a file. “w” allows to open the file into writing mode, and it also helps to overwrite the file.

 open('drasticcode.txt', 'w')

1. Python write list to file

  • Define a list
  • Create a text file named ‘drasticcode.txt’ and open it in writing mode 
  • Use iterator to write all the list items in a text file one by one without space.
NameList = ['Brian', 'Morgan', 'Anderson', 'Clark', 'Junaid'] 
a= open('drasticcode.txt', 'w')
for element in NameList:
    a.write(element)

To open the file inside the output console without opening any external file, write the below code with the above code:

file = open('drasticcode.txt', 'r')
print(file.read())

Output

Brian Morgan Anderson Clark Junaid

Or you can open it from the path where the file is created.

Python write list to file

output prints without space. To create the space and print every item on the new line, look at the below example of Python write list to file with newlines

2. Python write list to file with newlines

Python write list to file with newlines by following the below-given steps

  • Define a list.
  • Create a text file named “drasticcode.txt”.
  • “w” parameter allows opening file in writing mode.
  • Write all the elements of the list inside the file one by one by using an iterator.
NameList = ['Brian', 'Morgan', 'Anderson', 'Clark', 'Junaid'] 
a= open('drasticcode.txt', 'w')
for element in NameList:
    a.write("%s\n" % element )
file = open('drasticcode.txt', 'r')
print(file.read())

Output

Python write list to file

3. Python write list of numbers to file

  • Define a list of numbers.
  • Create and open a text file into writing mode.
  • Write the elements of the list inside the text file by using iterator.
  • Open the file into readability mode and print the file into the console (this is optional).

Note: if you do not want to print the same file in the console, do not use the last two lines of code and open your file into your default path.

NameList = [1,2,3,4,5,6,7,8,9,10] 
a= open('drasticcode.txt', 'w')
for element in NameList:
    a.write("%s\n" % element)

Output

1
2
3
4
5
6
7
8
9
10
Python write list to file

4. Python write list to file with brackets

Python write list to file with brackets with the help of below given simple method. This method prints a list inside a file in the same syntax of input.

list1 = ['Brian', 'Morgan', 'Anderson', 'Clark', 'Junaid'] 
with open('drasticcode.txt', 'w') as fp:
    print('Output list is: {}'.format(list1), file=fp) 
    print(file.read())

Output

Python write list to file without brackets

Python write list to file without brackets by using the “sep” method that separates each item of the list with a blank space as shown in the below output.

NameList = ['Brian', 'Morgan', 'Anderson', 'Clark', 'Junaid'] 
a = open('drasticcode.txt', 'w')
print(*NameList, sep = " ", file = a)
a.close()

Output

Python write list to file

Python write list to file as JSON

Python write list to file as JSON by using the dump() function that allows converting python objects into JSON objects. 

json.dump(list1, jsonfile): it will write the list1 content to jsonfile .

import json
list1 = ['Brian', 'Morgan', 'Anderson', 'Clark', 'Junaid'] 
with open('drasticcode.json', 'w') as jsonfile:
    json.dump(list1, jsonfile)
    file = open('drasticcode.json', 'r')
print(file.read())

Output

["Brian", "Morgan", "Anderson", "Clark", "Junaid"]

Python write list to CSV file

To write list to CSV file in python by opening a CSV (comma-separated-values) file into excel sheet first, 

  • Create four columns. 
  • Enter data inside the columns in a row vise manner.
  • Write the multiple lists into a CSV file.
  • Now open the “student.csv” file into the excel sheet.
import csv 
Details = ['Name', 'Subject', 'Language', 'Marks']  
rows= [['Brian', 'DSA', 'Python', 55],
       ['Morgan','DS', 'MATLAB', 89],
       ['Junaid', 'OSDC', 'C',60]]        
with open('student.csv', 'w') as f: 
    write = csv.writer(f) 
    write.writerow(Details) 
    write.writerows(rows)

Output

Python write list to file

Conclusion

In this article, we performed “Python write list to CSV, Python writes list to file as Json, Python write list of numbers to file, Python write list to file with newlines, Python write list to file without brackets” with simple examples of coding and their output.

Suggested Read: Remove Brackets from list Python

Leave a Comment