Created
July 17, 2019 04:01
-
-
Save cibofdevs/0c2179047f50c013acb17895c2aff094 to your computer and use it in GitHub Desktop.
This file contains 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 = 0 | |
for i in fileref: | |
num += len(i) | |
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. | |
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. | |
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. | |
f = open('school_prompt.txt', 'r') | |
beginning_chars = f.read(30) | |
print(beginning_chars) | |
# 5. Challenge: Using the file school_prompt.txt, assign the third word of every line to a list called three. | |
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. | |
fileref = open('school_prompt.txt', 'r') | |
words = fileref.read().split() | |
p_words = [word for word in words if 'p' in word] |
Could someone explain line 60 of the code to me, thanks..
p_words = []
with open('school_prompt.txt','r') as sp:
sp_1 = sp.read().split()
for word in sp_1:
if 'p' in word:
p_words.append(word)
print(p_words)
print("There are {} words that have p.".format(len(p_words)))
does anyone have the txt files? I already took the course and I'm refreshing my memory but I can't find the txt files and I don't want to pay just to have access to the files, it would be much appreciated :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My take on:
Challenge: Create a list called emotions that contains the first word of every line in emotion_words.txt.
file = open("emotion_words.txt", "r")
emotion_words = file.readlines() #read lines
print(emotion_words) #all lines appear
emotions = [] #new list to accumulate first word of every line
for line in emotion_words: #iterate every line separately in sequence
line = line.split() #split the line first to work each one independently
print(line) #first line appears
emotions.append(line[0]) #add the first word from the first line to the list emotions
print(emotions) #the first word appears
print(emotions)