Working With Txt Csv and Json Files in Python 1634092304
The document provides a comprehensive guide on how to read, create, and update TXT, CSV, and JSON files in Python. It includes code snippets for loading data into strings or lists, writing and appending data, and using the csv and json modules for handling CSV and JSON files. Key methods and best practices for file handling in Python are also highlighted.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
Working With Txt Csv and Json Files in Python 1634092304
The document provides a comprehensive guide on how to read, create, and update TXT, CSV, and JSON files in Python. It includes code snippets for loading data into strings or lists, writing and appending data, and using the csv and json modules for handling CSV and JSON files. Key methods and best practices for file handling in Python are also highlighted.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
FILES IN PYTHON
READ, CREATE AND UPDATE
TXT, CSV AND JSON FILES AGENDA 1. TXT FILES LOADING A TXT INTO A STRING USIGN.read() WRITING DATA TO TXT USING .write() This method will either create a new file or overwrite with open("file_name.txt") as file_object: an existing one with the same name variable_name = file_object.read() with open("file_name.txt", "w") as file_object: file_object.write(" text") o file_name.txt: this is the name of your txt file o file_object: it can be named as you want (it is a o file_name.txt: this is the name of your file. If it temporary variable), but remember to reference it doesn’t exist in your directory, Python will create in the line below a new file. If it exists, Python will overwrite it o variable_name: it can be named as you want. It is o text: the text you want to write to the txt file used to store the data from your .txt file as a (use \n in your text for line breaks) string
LOAD A TXT INTO A LIST USING.splitlines() APPEND DATA TO THE END OF A TXT FILE
with open("file_name.txt") as file_object: with open("file_name.txt", "a") as file_object:
list_name = file_object.read().splitlines() file_object.write(" text ")
o This will add the text to the end of an already
o list_name: name it as you want. It is a list used to existing .txt file (file_name.txt) store each line from your .txt
2 WORKING WITH FILES
2. CSV FILES o dict_name: name it as you want. It is used to store the data from your .csv file READING CSVs o newline = "": used so that we don’t accidentally There are two ways: mistake a line break in one of our data fields as a 1) Using .read() [not recommended] new row in our CSV with open("file_name.csv") as file_object: o print(list(dict_name)): this line will print the variable_name = file_object.read() data from the .csv file
o file_name.csv: this is the name of your csv file
o file_object: it can be named as you want, but IF THE DELIMITER IS NOT A COMMA remember to reference it in the line below import csv o variable_name: name it as you want. It is used to store the data from your .csv file as a string with open("file_name.csv", newline="") as file_object: dict_name=csv.DictReader(file_object, delimiter=";")
2) Using csv.DictReader() [highly recommended]
o delimiter=";": it sets the delimiter from a comma to import csv any character inside the quotes (; in this case)
with open("file_name.csv", newline="") as file_object: SAVE DATA TO CSV
dict_name = csv.DictReader(file_object) You first need a dictionary with the data you want to print(list(dict_name)) save to the csv file. You also need to declare the fieldnames (these are the key names of your dictionary)
3 WORKING WITH FILES
3. JSON FILES import csv READING A JSON FILE with open("file_name.csv", "w") as file_object: fields = [“key1", “key2"] import json
output_writter = csv.DictWriter(file_object, with open("file_name.json") as file_object:
fieldnames=fields) dict_name = json.load(file_object) output_writter.writeheader() o dict_name: it can be a dictionary with any name. It is for item in dict_list: used to store the data from your .json file output_writter.writerow(item) o "file_name.json": name of the file to read
o file_name.csv: this is the name of the csv file to
save the data WRITE DATA TO A JSON FILE
o fields: name of the keys in your dictionary (ex.
import json "name", "age") o dict_list: a list of dictionaries with the data you with open("file_name.json", "w") as file_object: json.dump(dict_name, file_object) want to save to the csv file. Example: dict_list = [{"name": "Claudio", "age": 26}, {"name": "Viktoria", "age": 22}] o dict_name: the name of the dictionary you want to load the json file. You can also use a list of dictionaries