I/O Operations exercices notebook
In [1]:
Copied!
file = open("example.txt", "r")
contents = file.read()
print(contents)
file.close()
file = open("example.txt", "r")
contents = file.read()
print(contents)
file.close()
hello everyone how are you doing Im doing very well this weekend thank you :)
In [2]:
Copied!
file = open("example.txt", "r")
first_line = file.readline()
print(first_line)
file.close()
file = open("example.txt", "r")
first_line = file.readline()
print(first_line)
file.close()
hello everyone how are you doing
In [3]:
Copied!
file = open("example.txt", "r")
lines = file.readlines()
print(lines)
file.close()
file = open("example.txt", "r")
lines = file.readlines()
print(lines)
file.close()
['hello everyone how are you doing\n', 'Im doing very well this weekend \n', 'thank you :) ']
In [5]:
Copied!
file = open("example2.txt", "w")
file.write("Hello, World!")
file.close()
file = open("example2.txt", "w")
file.write("Hello, World!")
file.close()
In [7]:
Copied!
file = open("example2.txt", "a")
file.write("\nHow are you today")
file.close()
file = open("example2.txt", "a")
file.write("\nHow are you today")
file.close()
In [8]:
Copied!
with open("example.txt", "r") as file:
contents = file.read()
print(contents)
with open("example.txt", "r") as file:
contents = file.read()
print(contents)
hello everyone how are you doing Im doing very well this weekend thank you :)
In [9]:
Copied!
import csv
import csv
In [10]:
Copied!
with open('data.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
# Loop over each row in the CSV file
for row in csv_reader:
# Access the values of each row
print(row)
with open('data.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
# Loop over each row in the CSV file
for row in csv_reader:
# Access the values of each row
print(row)
['To Kill a Mockingbird', 'Harper Lee', 'Grand Central Publishing', 'July 11 1960', '9780446310789'] ['1984', 'George Orwell', 'Signet Classic', 'June 1 1961', '9780451524935'] ['The Great Gatsby', 'F. Scott Fitzgerald', 'Scribner', ' April 10 1925', '9780743273565'] ['Pride and Prejudice', 'Jane Austen', 'Penguin Classics', 'January 28 1813', '9780141439518'] ['The Catcher in the Rye', 'J.D. Salinger', 'Little Brown and Company', 'July 16 1951', '9780316769174']
In [11]:
Copied!
with open('data2.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
# Write rows to the CSV file
csv_writer.writerow(['Title', 'Author', 'Publisher'])
csv_writer.writerow(['To Kill a Mockingbird', 'Harper Lee', 'Grand Central Publishing'])
with open('data2.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
# Write rows to the CSV file
csv_writer.writerow(['Title', 'Author', 'Publisher'])
csv_writer.writerow(['To Kill a Mockingbird', 'Harper Lee', 'Grand Central Publishing'])
In [12]:
Copied!
import csv
with open('data2.csv', 'a', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
# Append rows to the CSV file
csv_writer.writerow(['1984', 'George Orwell', 'Signet Classic'])
import csv
with open('data2.csv', 'a', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
# Append rows to the CSV file
csv_writer.writerow(['1984', 'George Orwell', 'Signet Classic'])
In [14]:
Copied!
import csv
# Read in the CSV file and filter out the rows that match a specific criteria
with open('data2.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
filtered_rows = []
for row in csv_reader:
if row[0] != '1984':
filtered_rows.append(row)
# Write the filtered rows back to the CSV file
with open('data2.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
for row in filtered_rows:
csv_writer.writerow(row)
import csv
# Read in the CSV file and filter out the rows that match a specific criteria
with open('data2.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
filtered_rows = []
for row in csv_reader:
if row[0] != '1984':
filtered_rows.append(row)
# Write the filtered rows back to the CSV file
with open('data2.csv', 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
for row in filtered_rows:
csv_writer.writerow(row)
In [ ]:
Copied!