-
-
Save cibofdevs/0c2179047f50c013acb17895c2aff094 to your computer and use it in GitHub Desktop.
# 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] |
#solution of last question
file = open('school_prompt.txt','r')
p_words = []
for line in file:
x = line.split()
for item in x:
if 'p' in item:
p_words = p_words + [item]
print(p_words)
Hi, please can anyone explain me, why we should put the for loop inside the bracket of the list [ ]
i did this code but its' did not work :
fileref = open("school_prompt.txt","r")
p_words = []
words = fileref.read().split()
for word in words:
if 'p' in word :
p_words.append(word)
print(p_words)
fileref.close()
but this one works :
fileref = open('school_prompt.txt', 'r')
words = fileref.read().split()
p_words = [word for word in words if 'p' in word]
please what's the difference between them,
Thanks
Hi, please can anyone explain me, why we should put the for loop inside the bracket of the list [ ]
i did this code but its' did not work :fileref = open("school_prompt.txt","r")
p_words = []
words = fileref.read().split()
for word in words:
if 'p' in word :
p_words.append(word)
print(p_words)
fileref.close()but this one works :
fileref = open('school_prompt.txt', 'r')
words = fileref.read().split()
p_words = [word for word in words if 'p' in word]please what's the difference between them,
Thanks
fileref = open('school_prompt.txt', 'r')
words = fileref.read().split()
p_words = []
for w in words:
if 'p' in w:
p_words.append(w)
print(p_words)
fileref.close()
................................................................XXXXX...................................
try this one
I guess the spacing is the issue in your case
Hi, please can anyone explain me, why we should put the for loop inside the bracket of the list [ ]
i did this code but its' did not work :fileref = open("school_prompt.txt","r")
p_words = []
words = fileref.read().split()
for word in words:
if 'p' in word :
p_words.append(word)
print(p_words)
fileref.close()but this one works :
fileref = open('school_prompt.txt', 'r')
words = fileref.read().split()
p_words = [word for word in words if 'p' in word]please what's the difference between them,
Thanks
p_words=[] may create an empty list but with a zero in it. So when you start appending new items it will get added after zero. So you better declare p_words=list()
"""
Using the file school_prompt.txt, assign the third word of every line to a list called three.
"""
with open('school_prompt.txt', 'r') as fileref:
three = [line.split()[2] for line in fileref]
print(three)
This solution uses list comprehension.
"""
Using the file school_prompt.txt, if the character ‘p’ is in a word, then add the word to a list called p_words.
"""
with open('school_prompt.txt', 'r') as fileref:
p_words = [word for line in fileref for word in line.split() if 'p' in word]
print(p_words)
filere = open('school_prompt.txt', 'r')
words = fileref.read().split()
p_words = [word for word in words if 'p' in word]
I got error in this code can you tell me why?
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)
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 :)
file1 = open("travel_plans.txt","r")
num = 0
for i in file1:
num += len(i)
file1.close()