Last active
August 7, 2020 00:53
-
-
Save guestPK1986/3d90e26835224622e469e642501f286d to your computer and use it in GitHub Desktop.
course_2_assessment_1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 1. The textfile, travel_plans.txt, contains the summer travel plans for someone with some commentary. | |
| # Find the total number of characters in the file and save to the variable num. | |
| fileref = open("travel_plans.txt","r") | |
| num =len(file.read()) | |
| fileref.close() | |
| # 2. We have provided a file called emotion_words.txt that contains lines of words that describe emotions. | |
| # Find the total number of words in the file and assign this value to the variable num_words. | |
| file = open("emotion_words.txt", "r") | |
| data = file.read() | |
| words = data.split() | |
| num_words = len(words) | |
| print(num_words) | |
| #OR | |
| num_words = 0 | |
| fileref = "emotion_words.txt" | |
| with open(fileref, 'r') as file: | |
| for line in file: | |
| num_words += len(line.split()) | |
| print("number of words : ", num_words) | |
| # 3. Assign to the variable num_lines the number of lines in the file school_prompt.txt. | |
| file=open("school_prompt.txt","r") | |
| num_lines =len(file.readlines()) | |
| #OR | |
| num_lines = sum(1 for line in open('school_prompt.txt')) | |
| # 4. Assign the first 30 characters of school_prompt.txt as a string to the variable beginning_chars. | |
| file = open('school_prompt.txt', 'r') | |
| beginning_chars = file.read(30) | |
| print(beginning_chars) | |
| file.close() | |
| # 5. Challenge: Using the file school_prompt.txt, assign the third word of every line to a list called three. | |
| fileref = open ("school_prompt.txt","r") | |
| line = fileref.readlines() | |
| three = [] | |
| for words in line: | |
| word = words.split() | |
| three.append(word[2]) | |
| print (three) | |
| #OR | |
| three = [] | |
| with open('school_prompt.txt', 'r') as f: | |
| three = [line.split()[2] for line in f] | |
| print(three) | |
| # 6. Challenge: Create a list called emotions that contains the first word of every line in emotion_words.txt. | |
| fileref = open ("emotion_words.txt","r") | |
| line = fileref.readlines() | |
| emotions = [] | |
| for words in line: | |
| word = words.split() | |
| emotions.append(word[0]) | |
| print (emotions) | |
| # 7. Assign the first 33 characters from the textfile, travel_plans.txt to the variable first_chars. | |
| f = open('travel_plans.txt', 'r') | |
| first_chars = f.read(33) | |
| print(first_chars) | |
| # 8. Challenge: Using the file school_prompt.txt, if the character ‘p’ is in a word, then add the word to a list called p_words. | |
| file = open("school_prompt.txt", "r") | |
| data = file.read() | |
| p_words = [] | |
| words = data.split() | |
| for word in words: | |
| if 'p' in word: | |
| p_words.append(word) | |
| print(p_words) | |
| file.close() | |
| #OR | |
| fileref = open('school_prompt.txt', 'r') | |
| words = fileref.read().split() | |
| p_words = [word for word in words if 'p' in word] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment