delimiting text file??

John Farrell jfarrell at mincom.com
Wed Oct 13 19:31:43 EDT 1999


Jim Bruer wrote:
> I've made it through the first 3 chapters of, Learning Python and
> various online tutorials. With some excitement I'm trying to write my
> FIRST program which will read a text file and comma delimit it, so it
> can be read into a database table. I can read and write the file (line
> by line) ok, but:
> 
> How do you insert a delimiter into a string at a given position?
> 
> Tried "f.insert(i,x)" then realized this is for mutable sequences.This
> is so stupid I'm embarrassed to ask.

Jim, once you've got the values to be separated into an array, you
can use string.join to put commas between them. Does that help?

--- csv.py ---
import string

def csv(filename):
    lines = open(filename).readlines()
    for line in lines:
        fields = string.split(line)
        print string.join(fields, ',')

csv('csv.py')
------

John




More information about the Python-list mailing list