""" quick_dirty_reader.py This program demonstrates how you can quickly import a text file into a Python program where you can split it up into tokens and do analysis on it. """ __author__ = "Richard White" __version__ = "2022-11-30" # read all of the lines into memory with open('data.txt',encoding='utf-8') as infile: # use list comprehension # rstrip() cleans off the newline character on each line lines = [line.rstrip() for line in infile] # Now we can... # 1. print each line for line in lines: print(line) # 2. or take a line and separate it into commas-separated values for word in lines[0].split(','): print(word) # 3. or maybe they're numbers instead? Go through and make a list # of the numbers nums = [] for word in lines[0].split(','): nums.append(int(word)) # 4. Now do something with the numbers print("Adding up all the numbers!") total = 0 for i in range(len(nums)): total += nums[i] print(total)