DATA 301 Introduction to Data Analytics - Python Data ...
DATA 301 Introduction to Data Analytics
Python Data Analytics
Dr. Ramon Lawrence University of British Columbia Okanagan
ramon.lawrence@ubc.ca
DATA 301: Data Analytics (2)
Python File Input/Output
Many data processing tasks require reading and writing to files.
Open a file for reading:
I/O Type
infile = open("input.txt", "r")
Open a file for writing: outfile = open("output.txt", "w")
Open a file for read/write:
myfile = open("data.txt", "r+")
DATA 301: Data Analytics (3)
Reading from a Text File (as one String)
infile = open("input.txt", "r")
val = infile.read() print(val)
Read all file as one string
infile.close()
Close file
Reading from a Text File (line by line)
infile = open("input.txt", "r") for line in infile:
print(line.strip('\n')) infile.close()
DATA 301: Data Analytics (4)
# Alternate syntax - will auto-close file with open("input.txt", "r") as infile:
for line in infile: print(line.strip('\n'))
Writing to a Text File
DATA 301: Data Analytics (5)
outfile = open("output.txt", "w")
for n in range(1,11): outfile.write(str(n) + "\n")
outfile.close()
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- data analytics certification
- data analytics software
- data analytics pdf
- data analytics free certification
- data analytics online courses
- data analytics research paper
- data analytics job description
- data analytics course
- data analytics certification online free
- online data analytics certificate program
- cornell data analytics certificate
- best data analytics certification