Johnnerz Johnnerz - 1 year ago 124
Python Question

Reading a text file and splitting it into single words in python

So I have this text file made up of numbers and words, for example like this - "09807754 18 n 03 aristocrat 0 blue_blood 0 patrician" and I want to split it so that each word or number will come up as a new line.

A whitespace separator would be ideal as I would like the words with the dashes to stay connected.

This is what I have so far:

f = open('words.txt', 'r')

for word in f:


not really sure how to go from here, I would like this to be the output:


Thanks for the help.

Answer Source

If you do not have quotes around your data:

with open('words.txt','r') as f:
    for line in f:
        for word in line.split():
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download