0% found this document useful (0 votes)
34 views2 pages

Reading and Writing To File

This document contains code snippets for writing to and reading from files, as well as sorting arrays. The writeToFile function writes data to a CSV file. The readFromFile functions read data from a CSV file either by using the CSV module or splitting lines. The sort function sorts an array using a bubble sort algorithm. The swop function swaps the positions of two elements in an array.

Uploaded by

Ali Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Reading and Writing To File

This document contains code snippets for writing to and reading from files, as well as sorting arrays. The writeToFile function writes data to a CSV file. The readFromFile functions read data from a CSV file either by using the CSV module or splitting lines. The sort function sorts an array using a bubble sort algorithm. The swop function swaps the positions of two elements in an array.

Uploaded by

Ali Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Writing to File

NB!!! import csv


def writeToFile (varA, varB...):
inFile = open ("name.csv", "w")
inFile.write (...)
inFile.close ()
return


Reading from File (csv)

def readFromFile (col):
A = []
inFile = open("name.csv", "r")
inCSVFile = csv.reader(inFile)
for value in inCSVFile:
a = float (value [col])
A.append (a)
inFile.close ()
return A


Reading from File (normal)

def readFromFile (col):
A = []
inFile = open("name.csv", "r")
for line in inFile:
data = line.split (",")
A.append (float(data[col]))
inFile.close ()
return A


Sorting Arrays

def sort (A):
size = len (A)
for i in range (0, size-1):
for j in range(i+1, size):
if A[j]> A[i]:
swop (A,i,j)
return A



Swop

def swop (A,a,b):
temp = A[a]
A[a] = A[b]
A[b] = temp

You might also like