Created
March 16, 2020 10:34
-
-
Save mayankdawar/23e65460e1e0797a0a021d076bc88217 to your computer and use it in GitHub Desktop.
Write code to find out how many lines are in the file emotion_words.txt as shown above. Save this value to the variable num_lines. Do not use the len method.
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
#Write code to find out how many lines are in the file emotion_words.txt as shown above. Save this value to the variable num_lines. Do not use the len method. | |
file = open("emotion_word.txt","r") | |
count = 0 | |
for aline in file.readlines(): | |
count += 1 | |
num_lines = count |
thank you!
#To create a string called first_forty comprised of the first 40 characters of the file "emotion_words2.txt,"
python
Copy code
Open the file in read mode and read the first 40 characters
with open("emotion_words2.txt", "r") as file:
first_forty = file.read(40)
Print the first 40 characters (optional)
print("First 40 characters:", first_forty)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"""
Write code to find out how many lines are in the file emotion_words.txt as shown above. Save this value to the variable num_lines. Do not use the len method.
"""
I like generator comprehensions because they yield each item out of the expression, one by one. And because of it, it saves a lot of memory.
https://stackoverflow.com/questions/364802/how-exactly-does-a-generator-comprehension-work