0

So guys here is my situation.

There is text file.Which contains some strings.They are all comma separated.
What i wanna do is just read the text file and print individually those strings which are comma separated.


Suppose the text file contains:

dog,cat,bird,good,bad,blah,blah,blah etc....

The python script should print


dog
cat
bird
good
bad
blah

etc
etc
etc
...
...
..

5
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by vegaseat
Featured Replies
  • 1

    This one is very good: [url]http://docs.python.org/tutorial/inputoutput.html[/url] Read More

  • Try this [CODE] >>> print "\n".join([x for x in "dog,cat,bird,good,bad,blah,blah,blah".split(",")]) dog cat bird good bad blah blah blah >>> [/CODE] Read More

1

Try this

>>> print "\n".join([x for x in "dog,cat,bird,good,bad,blah,blah,blah".split(",")])
dog
cat
bird
good
bad
blah
blah
blah
>>>
0

Maybe a little confusing that line from richieking.
You should read link from tony because this i basic stuff you shold be able to figure out.

with open('your.txt') as f:
    for item in f:
        from_file = item.split(',')

for line in from_file:
    print line

Edited by snippsat: n/a

0

Just in case you have an older version of Python ...

test_text = "dog,cat,bird,good,bad,blah1,blah2,blah3"

# let's save the test text to a file ...
fname = "aatest.txt"
fout = open(fname, "w")
fout.write(test_text)
fout.close()

# now you can read it back and print it ...
fin = open(fname)
text = fin.read()
fin.close()
for word in text.split(','):
    print word

''' result ...
dog
cat
bird
good
bad
blah1
blah2
blah3
'''
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.