Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Files 2023 4 15
- # FILES!!!
- # READ MODE
- # filename = input()
- # with open("test.txt", "r") as f:
- # # readlines() is a useful file object method
- # contents = f.readlines() # list of strings, line by line
- #
- # print(contents)
- # print(type(contents).__name__)
- # # for loop
- # for line in contents:
- # # line = line.strip()
- # print(line, end="") # print(line, end="\n")
- # print()
- # print("This should start on a clean new line!")
- # CSV Module
- import csv # csv module is reader()
- with open("mock_data.csv", "r") as f1:
- # contents = f.readlines() # list of strings, line by line
- contents = list(csv.reader(f1)) # csv.reader(f1, delimiter="\t")
- # print(contents[:30])
- # WRITE MODE
- with open("output_data.csv", "w") as f2:
- for row in contents:
- # email is row[3]
- # how to check end of email string? 1) slice 2) str endswith() 3) IN keyword
- if row[3][-4:] == ".gov": # if row[3].endswith(".gov"):
- # print(row) # just checking! Slice is good and data looks good
- # write() method takes one single str argument
- f2.write(",".join(row)+"\n") # is that enough...?
- # for method in dir(str):
- # if not method.startswith("_"):
- # print(f"{method}()")
- # APPEND MODE
- # with open("append_to_this.txt", "r") as f3:
- # contents = f3.readlines()
- # print(contents) # ['Frodo\n', 'Sam\n', 'Merry\n']
- with open("append_to_this.txt", "a") as f3:
- # f3.write("Pippin\n")
- # f3.write("Aragorn\n")
- # f3.write("Gandalf\n")
- f3.write("Gollum\nGlorfindel\nRosie Cotton\n") # you can call write() multiple times, or just add all those strings together
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement