File IO
Download Numpy and pandas Library
3) Install the Wheel Package
➢ Online Installation --------------------------------------- Terminal Command:
Terminal Command: > pip install numpy-1.21.4-cp37-cp37m-
win32.whl
> pip install numpy
> pip install pandas To install pandas Lib, your should install all it’s
dependencies listed bellow:
Note: pip is Package manager, it's main source is PyPI.org - numpy
Note: to access pip.exe in cmd / Pycharm - six
Terminal add the following path into the windows paths - python-dateutil
- pytz
C:\Users\UserName\AppData\Local\Progra - pandas
ms\Python\Python37\Scripts - et-xmlfile
- openpyxl
➢ Offline Installation ---------------------------------------
1) Refer to PyPI.org Read text files:
PyPI: Python Package Index
ID = open(file_path, mode),
Official third-party software repository for Python
mode: r, w, a (+, b), b: binary
2) find the correct Package of your desired library
r r+ w w+ a a+
Note: Python Package are stored in .whl files
read * * * *
- Wheel Binary Package Format (PEP 427) write * * * * *
- Wheel Packages naming convention (PEP 425): create * * * *
{Library Name}-{version}-{Python tag}- Overwrite * *
{ABI tag}-{Platform tag}.whl position at start * * * *
{Python tag}
position at end * *
Determines which Python Interpreter:
▪ CPython (most common)
▪ PyPy (faster than CPython but not compatible with all # ID = open("File1.txt", "w")
codes) # ID.close()
▪ Jython (Written in java)
▪ IronPython (Written in C#) Read all text ------------------------
F1 = open("File1.txt", 'r')
{ABI tag} Str = F1.read()
• cp37m, cp37dm, cp37d (m:compiled with pymalloc,) print(Str + '\n\n')
• abi3 F1.close()
• none
{platform tag}: Read based on number of bytes --------
• win32, win_amd64
F2 = open("File1.txt", 'r')
• linux_i386, linux_x86_64 Str1 = F2.read(4)
• macosx_10_13_x86_64 Str2 = F2.read(5)
print(Str1 + '| |' + Str2)
for example: Str3 = F2.read()
Numpy for Python 3.7 and Win32: Str4 = F2.read()
numpy-1.21.4-cp37-cp37m-win32.whl
Python course By: Mansour Torabi
File IO
print(Str3) Cfile.close()
print(Str4) # Empty String, reading
cursor in end of the file
F2.close() with open('csvF1.csv') as F1:
CR = csv.reader(F1)
Read Lines ------------------------ R1 = []
for r in CR:
F1 = open("File1.txt",'r') R1.append(r)
print(F1.readlines()) # Output: List (of print(R1)
each line)
F1.close() Write csv files:
Writing Files ---------------------- header = ['Name', 'Score1', 'Score2']
data = [['Ali', 19, 20], ['Hasan', 18,
F2 = open("File1.txt", 'a') # if 'w', 19], [‘Hamed', 17, 19]]
contents will be Overwrite (deleted)
F2.write("Line 4\n")
print(F2.write("Line 5\n")) # return no filename = 'Students.csv'
of written bytes with open(filename, 'w') as file:
F2.close() csvwriter = csv.writer(file)
csvwriter.writerow(header)
print(open("file1.txt").read()) csvwriter.writerows(data)
try:
F = open("file1.txt") 2) Using pandas Lib
print(F.read())
except: Read csv files:
raise
finally: import pandas as pd
F.close()
DF = pd.read_csv('csvF1.csv')
# With statement [Alternative for try- print(DF)
finally]: Pythonic Style
with open("file1.txt") as FI1: # Write csv files:
Automatically close the file after
execution header = ['Name', 'Score1', 'Score2']
print(FI1.readlines()) data = [['Ali', 19, 20], ['Hasan', 18,
19], [‘Hamed', 17, 19]]
Read / Write csv files: data = pd.DataFrame(data, columns=header)
data.to_csv('Stu.csv', index=False)
1) Using Standard Python Lib
Read csv files:
Read Excel Files (Using pandas library)
import csv import pandas as pd
Cfile = open('csvF1.csv') DF2 = pd.read_excel('adf.xlsx')
csvreader = csv.reader(Cfile) print(DF2)
print(DF2.iloc[1]) # Row 1
rows = [] print(DF2.iloc[1:]) # Row 1 to end
for row in csvreader:
rows.append(row)
print(rows)
Python course By: Mansour Torabi