Advertisement
jspill

webinar-python-file-writing-2022-02-12

Feb 12th, 2022
1,493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. # Working with Files - Reading and Writing
  2. # Sat Feb 12
  3.  
  4. # opening a file
  5. # older syntax
  6. # f = open("test.txt", "r") # r, w, a, and r+, w+, a+... don't worry about those
  7. # # work with the file
  8. # f.close()
  9.  
  10. # newer way, the with/open() block
  11. with open("test.txt", "r") as f: # r is default mode, but I'll include
  12.     # work with file
  13.     # 2 ways I normally grab the contents of file
  14.     # read() # returns the entire contents of the file, as one big string
  15.     # readlines() # returns a list of strings, kinda like str.split()
  16.     myList = f.readlines()
  17.  
  18. # when you leave the block, the file is closed
  19. # in read mode, you might as well close as soon as possible to save memory
  20. # I still have myList
  21. print(myList)
  22.  
  23. for line in myList:# line here is a str in the list
  24.     # print(type(line))
  25.     # print("Is string?", isinstance(line, str))
  26.     # line = line.rstrip() # strip()
  27.     print(line, end="-") # print(end="\n")
  28.     # if I want update the list
  29.     # myList[x]
  30.     myList[myList.index(line)] = line.rstrip()
  31. print()
  32. print(myList)
  33.  
  34. print("---------------")
  35. # we could use the csv module and its csv.reader() instead
  36. import csv
  37. # with open("mock_data.csv", "r") as cFile:
  38. #     myList = cFile.readlines()
  39. #
  40. # print(myList)
  41. # for line in myList:
  42. #     # line = line.rstrip()
  43. #     row = line.rstrip().split(",")
  44. #     print(row[3])
  45. with open("mock_data.csv", "r") as cFile:
  46.     myVar = csv.reader(cFile, delimiter=",")
  47.     myVar = list(myVar) # I like to recast a csv.reader file as a list...
  48.  
  49. print(myVar) # Look! It's a LIST of LISTS
  50.  
  51. # with open()...
  52. # write() # in "w" mode, destructive; in "a" mode it appends to end
  53.  
  54. # Let's look at the first part of  Lab 29.1
  55. filename = input().rstrip()
  56. # open in read
  57. with open(filename, "r") as f:
  58.     # myList = f.readlines()
  59.     myList = [line.rstrip() for line in f.readlines()]
  60. # print(myList) # looks good!
  61. tvDict = {}
  62. # for item in myList: # but range() would be better to get index
  63. for i in range(0, len(myList), 2):
  64.     key = myList[i]
  65.     # value = myList[i+1]
  66.  
  67.     if key in tvDict:  # if it's ALREADY in there
  68.         tvDict[key] += "; {}".format(myList[i + 1])
  69.     else:
  70.         tvDict[key] = myList[i + 1]  # so this is the normal thing to do...
  71. print(tvDict)
  72.  
  73. # write output_keys.txt
  74. with open("output_keys.txt", "w") as f:
  75.     for k in tvDict:
  76.         # print("{}: {}\n".format(k, tvDict[k])) # writing looks the same...
  77.         f.write("{}: {}\n".format(k, tvDict[k])) # it's just strings!
  78. # That's MOST of writing out the output_keys.txt file they asked for, but...
  79. # In unit testing you will get a curve ball: you have to control for
  80. # "zero padded" numbers vs not: "07" vs "7" in your keys
  81.  
  82.  
  83. # if you're ever reading and writing in the same open() block...
  84. f.seek() # moves to position in filestream you're reading/writing, like a scrubber
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement