Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:     print(word) 

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

09807754 18 n 3 aristocrat ... 
like image 517
JohnConneely Avatar asked Jun 04 '13 15:06

JohnConneely


People also ask

How do you read and split a file in Python?

Use open with readline() or readlines() . The former will return a line at a time, while the latter returns a list of the lines. Use split(delimiter) to split on the comma.

How do you split a file into chunks in Python?

To split a big binary file in multiple files, you should first read the file by the size of chunk you want to create, then write that chunk to a file, read the next chunk and repeat until you reach the end of original file.


2 Answers

Given this file:

$ cat words.txt line1 word1 word2 line2 word3 word4 line3 word5 word6 

If you just want one word at a time (ignoring the meaning of spaces vs line breaks in the file):

with open('words.txt','r') as f:     for line in f:         for word in line.split():            print(word)     

Prints:

line1 word1 word2 line2 ... word6  

Similarly, if you want to flatten the file into a single flat list of words in the file, you might do something like this:

with open('words.txt') as f:     flat_list=[word for line in f for word in line.split()]  >>> flat_list ['line1', 'word1', 'word2', 'line2', 'word3', 'word4', 'line3', 'word5', 'word6'] 

Which can create the same output as the first example with print '\n'.join(flat_list)...

Or, if you want a nested list of the words in each line of the file (for example, to create a matrix of rows and columns from a file):

with open('words.txt') as f:     matrix=[line.split() for line in f]  >>> matrix [['line1', 'word1', 'word2'], ['line2', 'word3', 'word4'], ['line3', 'word5', 'word6']] 

If you want a regex solution, which would allow you to filter wordN vs lineN type words in the example file:

import re with open("words.txt") as f:     for line in f:         for word in re.findall(r'\bword\d+', line):             # wordN by wordN with no lineN 

Or, if you want that to be a line by line generator with a regex:

 with open("words.txt") as f:      (word for line in f for word in re.findall(r'\w+', line)) 
like image 60
dawg Avatar answered Sep 23 '22 21:09

dawg


f = open('words.txt') for word in f.read().split():     print(word) 
like image 28
dugres Avatar answered Sep 25 '22 21:09

dugres