User Tools

Site Tools


python

Table of Contents

Python

Read Csv

  • import csv
  • file = open('Emp_Info.csv', 'r')
  • try:
  • reader = csv.reader(file)
  • for each_row in reader:
  • print(each_row)
  • finally:
  • file.close()

Lists

  • create list - stock = []
  • append to list - stock.append(“fred”)

Pip

  • python -m pip install somePackage
  • sudo python -m pip install email_to ←—————— does not work

error: externally-managed-environment

This environment is externally managed To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install.

If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv.

Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed.

For more information visit http://rptl.io/venv

Files

  • open file - input = (“someFileLocation”, “r”)
  • read a line - line = input.readline()
file_path = 'randomfile.txt'
file_text = open(file_path, "r")
a = True
while a:
    file_line = file_text.readline()
    if not file_line:
        print("End Of File")
        a = False
file_text.close()
file = open("randomfile.txt", "r")

while (f := file.read()):
    process(f)

file.close()
python.txt · Last modified: 2024/10/31 15:57 by 127.0.0.1