next line, new line

Jeremy Bowers jerf at jerf.org
Sun Jan 30 23:42:19 EST 2005


On Sun, 30 Jan 2005 20:21:49 -0800, rasdj wrote:

> Thanks Jeremy, something like this would work:
> 
> try:
> lines = [ line.replace(",\n;", ")\n;") for line in input  ]
> 
> If I could figgure out how to:
> 
> IF ':' in line
> READ next line in
> lines = [ line.replace(",\n;", ")\n;") for line in input  ]
> output.write(str.join('', lines))
> 
> because there are lots of "comma newline" but the only ones I want are
> the ones that follow the semicolon.
> 
> RasDJ

My apologies, I was unclear when I said "suck the whole file in"; a little
too colloquial.

-------------

filename = "YOUR_FILENAME_HERE.sql"
output = "YOUR_DESTINATION_HERE.sql"
f = open(filename)
contents = f.read()
f.close()

contents = contents.replace(",\n;", ")\n;")
# optionally, the \n in the second string may be dropped, it isn't
# necessary

f = open(output, "w")
f.write(contents)
f.close()

------------

In other words, no mucking around with "lines" at all. You're better off
thinking of the file as a flat stream of bytes in this case.




More information about the Python-list mailing list