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
...
...
..

Recommended Answers

All 4 Replies

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
>>>

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

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
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.