Python script to read a text file and write into a csv file
Question: I have a text file in which each row has multiple words (which I want to
consider as columns). Now I want to read all the data from this text file and
create a csv file with rows and columns. I am written the code till here -

    
    
    import csv
    f=open("text.txt", "r")
    reader=csv.reader(f)
    offile=open("output.csv","wb")
    writer=csv.writer(offile,delimiter='\t',quotechar='"',quoting=csv.QUOTE_ALL)
    for row in reader:
     ........
    
    f.close()
    offile.close()
    

I am not able to understand how to divide each row into columns and write this
columns and rows back while writing a csv file? I am a newbie to python, so a
good example I will be very greatful.

Thanks


Answer: Try splitting the lines via a regular expression:

    
    
    line = "Foo bar baz quux"
    import re
    pieces = re.split("\s+", line)
    print pieces
    

This results in

`['Foo', 'bar', 'baz', 'quux']`

The regular expression used above matches for multiple (+) white space
characters (\s)

