CSV or (Comma Separated Values) file is a broadly used format for storing and exchanging tabular information. It’s related/equal to a spreadsheet or a database however easier and extra versatile. Python offers numerous modules and strategies to take care of CSV information, such because the CSV module, to_csv() perform, and many others. These strategies can simply write information from a listing or dictionary to a CSV file.
This weblog will present an intensive information on the right way to create a CSV file from a Python checklist by using the next strategies:
Technique 1: Create CSV File From a Python Listing Utilizing “write()” Technique
The “open()” perform is used together with the nested for loop and “file.write()” technique to create a CSV from a Python checklist. For additional understanding, contemplate the next code:
list1 = [[‘Name’,‘Joseph’, ‘Anna’, ‘Lily’],[‘Age’, 12, 32, 22],[‘Height’, 5.7, 5.3, 4.9]]
with open(‘new.csv’, ‘w’) as file:
for i in list1:
for j in i:
file.write(str(j) + ‘,’)
file.write(‘n‘)
Within the above code:
- The checklist is initialized, and the “open()” perform is employed to open the “CSV” or comma-separated file in “w” mode.
- The nested for loop iterates over the checklist and writes the checklist ingredient to CSV utilizing the “write()” technique.
Output
The checklist has been transformed right into a Python CSV file.
Technique 2: Create a CSV File From a Python Listing Utilizing the “CSV” Module
The “csv” module helps numerous features to learn and write CSV information. The “csv.author()” perform is used with the “writerows()” perform to create a author object and write rows of knowledge to that object. Let’s overview the next code for additional understanding:
import csv
list1 = [[‘Name’,‘Joseph’, ‘Anna’, ‘Lily’],[‘Age’, 12, 32, 22],[‘Height’, 5.7, 5.3, 4.9]]
with open(‘new.csv’, ‘w’, newline =”) as file:
f = csv.author(file)
f.writerows(list1)
Right here on this code:
- The “CSV” module is known as/imported, and the checklist is initialized.
- The “open()” perform is utilized to open the comma-separated file named “csv” in “w” mode.
- The “author()” perform takes the file object as an argument and creates the author object.
- Lastly, the “writerows()” perform is used to write down the desired checklist to a CSV file named “new.csv”.
Output
The CSV file has been created from the Python checklist.
Technique 3: Create a CSV File From a Python Listing Utilizing the “to_csv()” Perform
The “to_csv()” perform of the Pandas module is used to transform the Pandas DataFrame to a CSV file. This perform is utilized to assemble/create a CSV file from a Python checklist. For demonstration, take the next code:
import pandas
list1 = [[‘Name’,‘Joseph’, ‘Anna’, ‘Lily’],[‘Age’, 12, 32, 22],[‘Height’, 5.7, 5.3, 4.9]]
df = pandas.DataFrame(list1)
df.to_csv(‘new.csv’, index=False, header=False )
Within the above instance:
- The “pandas” module is imported, and nested lists are initialized.
- The “pandas.DataFrame()” perform creates DataFrame by accepting the initialized checklist.
- This DataFrame is transformed right into a CSV file utilizing the “to_csv()” perform.
Output
The Python checklist has been transformed to a CSV file.
Technique 4: Create CSV File From a Python Listing Utilizing the “savetxt()” Perform
The “savetxt()” perform of the Numpy module is used to avoid wasting or retailer an array to a textual content file. We are able to use this technique to create/assemble a CSV file. Right here is an instance code:
import numpy
list1 = [[1, 5, 10],[12, 32, 22],[5.7, 5.3, 4.9]]
arr = numpy.array(list1)
numpy.savetxt(‘new.csv’, arr, delimiter=‘,’, fmt=‘%d’)
Within the above code:
- The “Numpy” module is imported.
- The “array()” takes the checklist and retrieves an array object.
- The “savetxt()” takes the file title, array, and the “delimiter” parameter as an argument to create a CSV file from the checklist.
Word: This technique can solely write a listing with matching information sort values in any other case TypeError is raised.
Output
The CSV file has been created efficiently.
Conclusion
In Python, the “write()”, “CSV”, “to_csv()”, and the “savetxt()” strategies are utilized to create a CSV file from the enter checklist. The “write()” technique is used together with a for loop and open() perform to write down a CSV file from a Python checklist. The opposite strategies, such because the “CSV” module, “to_csv()” perform of the Pandas module, and “savetxt()” perform of the Numpy module, can effectively write a CSV file from the given Python checklist. This information introduced an entire information on Python checklist to CSV utilizing quite a few examples.