9 AnswersNew Answer
Okey! You can first use file.readlines() to read the lines in the file to items in a list. Then for every item in the list you use split to split the strings to lists. Then use pick out every first letter and put them together. Use a for loop to go through the list. And file is your file of course. myfile.readlines() => [tile1, title2,..] print(title1) >> Asdf jujj oki Split title1 => wdLst = title1.split() => [Asdf’, ’jujj’, ’oki’] Pick every first letter: res = ’’ for i in wdLst: res += wdLst[i] print(res) >> Ajo Etc ... Obs! Pseudo code! You have to translate it into Python. Take one step at a time!
It is a good start to explaine it in english, but with code it become more easy to understand how to help you. So the file contains one title per line? file f = ’Asdf jujj oki\nWer hygg\nGrujj hxg ugg’ When printing the three titles: ’Asdf jujj oki’ ’Wer hygg’ ’Grujj hxg ugg’ Should give this combinnation => ’AWG’ ? Or how do you mean?
a = "Yvonne Nelson\nBerla Monde\nNikki Samonas" b = a.split('\n') print(b) c = b.split() print(c) res = "" for wd in c: res += wd print(res)
No, I should be printing out "Ajo" "Wh" "Ghu"
This is what I'm arriving at but I want to print then in separate lines as in. "YN" "BM" "NS" https://code.sololearn.com/cqCfvvhSNN51/?ref=app
When I use the readlines(), it denies me from using the split For eg: msg = file.readlines() info = msg.split() Error saying list doesn't have an attribute split
titleLst = myfile.readlines() creates a list. The list contain your titles. You have to use split in the list items: titleLst[i].split(), where i is the i:th title in the list. titleLst.split() is the same as ’Asdf jujj oki’.split() and give you a new list [’Asdf’, ’jujj’, ’oki’]
The same error AttributeError: 'list' object has no attribute 'split'
Yes, because lists are the result when you use split. You should use split on the strings inside the list. Lst = [title0, title1, title2, ....] title_0 = Lst title_1 = Lst For example title_0 is a string with three words: ”Asdf jujj oki” This string object is okey to use split on: wdLst = ”Asdf jujj oki”.split() The result become a new list: [’Asdf’, ’jujj’, ’oki’]