+ 3
How to solve thid
You are given a file named "books.txt" with book titles, each on a separate line. To encode the book titles you need to take the first letters of each word in the title and combine them. For example, for the book title "Game of Thrones" the encoded version should be "GoT". Complete the program to read the book title from the file and output the encoded versions, each on a new line.
19 Respostas
+ 6
file is works like a pointer, having total file content. But split is string method.
You can use readlines() like
file = file.readlines() # to make a list of lines. Then you can apply split in loop.
Again you need to separate lines and line words..
edit:
may complicated before, reading about the methods you used.. ..
file = open("/usercode/files/books.txt", "r")
for words in file:
for i in words.split():
print(i[0],end="")
print()
file.close()
+ 7
Osmany Baluta ,
sure, people can say hello, but should not be in the *q&a* section.
> it can be done in the feed in the community section.
+ 6
Imran Shakil ,
here are 2 sources from sololearn to get more info about string method split():
> tutorial *python for beginners*, *functions*, lesson 39.1 / *string functions*
> https://www.sololearn.com/learn/Python/2456/?ref=app
+ 6
"thid" is a typo of "this"
Here, the original poster wants to know how to solve the task from one of the courses.
+ 5
Osmany Baluta ,
not clear what your issue or question is. please update your post with:
> a clear and complete task description
> a description that points to your issue, including a possible error messages
> link your code try here
if this is nof your intention, please use the personal feed in the *community* section.
+ 5
Arshadvlog ,
this is not the place to say hello.
> *q&a* section is meant for raising coding related questions and discussions.
+ 5
Navaneethan R ,
Haris ,
it would be better if you start your own question, because the current discussion was started by someon else. hence people may not be aware of your issue.
> this is also not the place for general comments.
> *q&a* section is meant for raising coding related questions and discussions.
+ 5
أحمد عبد الرازق
Jacinto Tejera González ,
it is better for you to start your own post, otherwise poeple will not get aware of you.
+ 3
Imran Shakil
May be you can find it in string lesson, string methods. But i think, may or may not detailly, not sure.
Other way : hope this may help...
https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_string_split.asp
+ 2
Open file. Read each line. and split into words. Print first letter of each word.
That's it. here what is your difficulty..? Lesson have example for each step. Revise once before attempting...
+ 2
In other way: by using loop, going through each character of string, print the letter after the space letter.
But with split :
if str1 is the string you read, then you can split into list of words as :
s = str1.split(" ") # split by space. now you can use loop to itearate each word and print first letter.
edit: Imran Shakil
an example for you :
str1 = "the split way"
for s in str1.split() : print( s[0], end="" )
this prints "tsw"
for any further, share your try so that it helps where you struck...
+ 1
Imran Shakil it would help if we could see your code.
How are you using split?
Copy your code, open up a new code in the code playground, paste, save and attach it here please.
+ 1
You need to readlines before you split them.
0
Jayakrishna🇮🇳.
I had never seen split() in my privious lessons.....I dont know how to use split().
This is the main problem
0
file = open("/usercode/files/books.txt", "r")
for line in file.readlines():
words = line.split()
encode =''
for word in words :
encode = encode + word[0]
print (encode)
file.close()
- 1
Jayakrishna🇮🇳.
Every time when I had trid split(), it shows me AttributeError and shows a message "The file object dose not have a split attribute. You proberbly want to split the contents of the file into a list of line".
- 1
Ausgrindtube
#here it is
file = open("/usercode/files/books.txt", "r")
for i in file.split():
print(i[0],end="")
file.close()
- 1
Jayakrishna🇮🇳
Thank you very much. My exam have been completed by your help.
I need your one more help...would you please tell me, where from I can learn albout split() by sololearn app?