Write Data to CSV
To faciliate writing to a CSV file, we’ll make use of the
csv library. If you want to add to an existing CSV file - rather than overwriting it - use the
a flag (append) instead of the
w flag (write).
import csv with open("<FILENAME>.csv", "w") as csv_file: writer = csv.writer(csv_file, delimiter = ";") writer.writerow(["<COLUMN_1>", "<COLUMN_2>"]) for content in all_data: writer.writerow([<DATAFRAME>["<COLUMN_1>"], <DATAFRAME>["<COLUMN_2>"])
If your data is already stored as a pandas DataFrame you can easily export it as follows:
Read CSV Data
read.csv() method from the
pandas library automatically converts the data into a DataFrame which provides rich functionalities for data analysis.
If you encounter CSV files with a custom delimiter (i.e., symbol used to separate the data into rows and columns), you can explicitly indicate that with the
sep parameter. For example, in this case the interpreter expects that data fields have been separated by semi-colons (
Write Data to JSON
json packages makes exporting raw JSON data (
import json with open("<NAME_OF_JSON_EXPORT>.json", "w") as outfile: json.dump(<JSON_FILE>, outfile)
Read JSON Data
In a similar way, you can import the JSON files with the same library.