I have txt file and I want to import text to list in Python. Txt file(If need I can change it): Hi, Hello, Welcome And I want list like that: ['Hi','Hello','Welcome']
Asked
Active
Viewed 4,022 times
-4
-
Eh I tryied somethink like that: – Szymon Dec 07 '16 at 19:20
-
filename = 'Test-file.txt' file=open(filename,'r') L1list = file.readline() – Szymon Dec 07 '16 at 19:20
-
@Simon edit your question with that code – depperm Dec 07 '16 at 19:21
-
Edit your question and show your attempt, don't put it in comments. – Mark Tolonen Dec 07 '16 at 19:21
-
2I put the exact headline into Google and got a ton of hits. – Klaus D. Dec 07 '16 at 19:21
-
Read about [`split()`](https://docs.python.org/3/library/stdtypes.html#str.split) and try to write some code! – ettanany Dec 07 '16 at 19:24
-
I am sorry if this duplicate :c – Szymon Dec 07 '16 at 19:26
2 Answers
0
You mean:
>>>hmm="Welcome And I want list like that"
>>>my_list=hmm.split()
>>>my_list
['Welcome', 'And', 'I', 'want', 'list', 'like', 'that']

mpruchni
- 290
- 5
- 17
0
Basically:
print open('yourfile.txt').read().split(',')
And if you're not sure about extra spaces or tabs in your source file, you can remove them:
print map(str.strip, open('yourfile.txt').read().split(','))

Organis
- 7,243
- 2
- 12
- 14