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-
[Link]
> 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 [Link] - numpy
Note: to access [Link] 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 [Link] 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("[Link]", "w")
codes) # [Link]()
▪ Jython (Written in java)
▪ IronPython (Written in C#) Read all text ------------------------
F1 = open("[Link]", 'r')
{ABI tag} Str = [Link]()
• cp37m, cp37dm, cp37d (m:compiled with pymalloc,) print(Str + '\n\n')
• abi3 [Link]()
• none
{platform tag}: Read based on number of bytes --------
• win32, win_amd64
F2 = open("[Link]", 'r')
• linux_i386, linux_x86_64 Str1 = [Link](4)
• macosx_10_13_x86_64 Str2 = [Link](5)
print(Str1 + '| |' + Str2)
for example: Str3 = [Link]()
Numpy for Python 3.7 and Win32: Str4 = [Link]()
[Link]
Python course By: Mansour Torabi
File IO
print(Str3) [Link]()
print(Str4) # Empty String, reading
cursor in end of the file
[Link]() with open('[Link]') as F1:
CR = [Link](F1)
Read Lines ------------------------ R1 = []
for r in CR:
F1 = open("[Link]",'r') [Link](r)
print([Link]()) # Output: List (of print(R1)
each line)
[Link]() Write csv files:
Writing Files ---------------------- header = ['Name', 'Score1', 'Score2']
data = [['Ali', 19, 20], ['Hasan', 18,
F2 = open("[Link]", 'a') # if 'w', 19], [‘Hamed', 17, 19]]
contents will be Overwrite (deleted)
[Link]("Line 4\n")
print([Link]("Line 5\n")) # return no filename = '[Link]'
of written bytes with open(filename, 'w') as file:
[Link]() csvwriter = [Link](file)
[Link](header)
print(open("[Link]").read()) [Link](data)
try:
F = open("[Link]") 2) Using pandas Lib
print([Link]())
except: Read csv files:
raise
finally: import pandas as pd
[Link]()
DF = pd.read_csv('[Link]')
# With statement [Alternative for try- print(DF)
finally]: Pythonic Style
with open("[Link]") as FI1: # Write csv files:
Automatically close the file after
execution header = ['Name', 'Score1', 'Score2']
print([Link]()) data = [['Ali', 19, 20], ['Hasan', 18,
19], [‘Hamed', 17, 19]]
Read / Write csv files: data = [Link](data, columns=header)
data.to_csv('[Link]', index=False)
1) Using Standard Python Lib
Read csv files:
Read Excel Files (Using pandas library)
import csv import pandas as pd
Cfile = open('[Link]') DF2 = pd.read_excel('[Link]')
csvreader = [Link](Cfile) print(DF2)
print([Link][1]) # Row 1
rows = [] print([Link][1:]) # Row 1 to end
for row in csvreader:
[Link](row)
print(rows)
Python course By: Mansour Torabi