The document discusses various methods for reading and writing files in Python including text files, CSV files, and Excel files. It covers opening and closing files, reading the entire file or portions of it based on bytes, reading lines into a list, writing to files, and using the with statement as an alternative to try-finally for file handling. It also introduces the Python Package Index (PyPI) and shows examples of installing packages and reading/writing CSV and Excel files using the csv and pandas modules.
The document discusses various methods for reading and writing files in Python including text files, CSV files, and Excel files. It covers opening and closing files, reading the entire file or portions of it based on bytes, reading lines into a list, writing to files, and using the with statement as an alternative to try-finally for file handling. It also introduces the Python Package Index (PyPI) and shows examples of installing packages and reading/writing CSV and Excel files using the csv and pandas modules.
# How to read text files ----------- print(open("file1.txt").read())
# Syntax: # ID = open( file_path , mode), mode: try: r, w, a (+, b) F = open("file1.txt") print(F.read()) except: r r+ w w+ a a+ raise read * * * * finally: F.close() write * * * * * create * * * * # With statement [Alternative for try- finally]: Pythonic Style Overwrite * * position at start * * * * with open("file1.txt") as FI1: # position at end * * Automatically close the file after execution print(FI1.readlines()) # ID = open("File1.txt", "w") # ID.close() # PyPI: Python Package Index Read all text ------------------------ # Syntax: pip install LibName F1 = open("File1.txt", 'r') # Online installation: pip install numpy Str = F1.read() # Offline installation: pip install print(Str + '\n\n') numpy-1.21.4-cp37-cp37m-win32.whl F1.close() # reading csv files Read based on number of bytes -------- import csv ------------------------------ F2 = open("File1.txt", 'r') Str1 = F2.read(4) import pandas as pd---------------------- Str2 = F2.read(5) print(Str1 + '| |' + Str2) # Writing csv file Str3 = F2.read() header = ['Name', 'Score1', 'Score2'] Str4 = F2.read() data = [['AA', 19, 20], ['BB', 18, 19], ['GG', 17, 19]] print(Str3) print(Str4) # Empty String, reading filename = 'Students_Data.csv' cursor in end of the file with open(filename, 'w', newline="") as F2.close() file: csvwriter = csv.writer(file) Read Lines ------------------------ csvwriter.writerow(header) csvwriter.writerows(data) F1 = open("File1.txt",'r') print(F1.readlines()) # Output: List (of # using Pandas--------------------- each line) header = ['Name', 'Score1', 'Score2'] F1.close() data = [['AA', 19, 20], ['BB', 18, 19], ['GG', 17, 19]] Writing Files ---------------------- data = pd.DataFrame(data, columns=header)
F2 = open("File1.txt", 'a') # if 'w', data.to_csv('Stu_data.csv', index=False)
contents will be Overwrite (deleted) F2.write("Line 4\n") # reading Excel files print(F2.write("Line 5\n")) # return no import pandas as pd of written bytes DF2 = pd.read_excel('adf.xlsx') F2.close() print(DF2) print(DF2.iloc[1])