473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

next line, new line

I have a lot of SQL to convert to postgres from oracle. I have most of
the problems worked out except for this last bit. Many of my tables
need the last comma replaced with a close parenthesis - they look like
this:

create table schema.table (
FLD000 NUMERIC(10,0) NOT NULL,
FLD001 CHAR(3) NOT NULL,
FLD002 DATE NOT NULL,
;

when the syntax requires:

FLD002 DATE NOT NULL)
;

I output the text in reverse thinking I could find the semicolon, go to
the next line and replace the 'comma newline' with 'closeparen newline'
and then go on to find the next semicolon.

;
FLD002 DATE NOT NULL,
FLD001 CHAR(3) NOT NULL,
FLD000 NUMERIC(10,0) NOT NULL,
create table schema.table (
;
FLD002 DATE NOT NULL,
FLD001 CHAR(3) NOT NULL,
FLD000 NUMERIC(10,0) NOT NULL,
create table schema.table2 (

I don't seem to be making any progress altho I have had some
interesting output.

Throw me a bone?

Thank you,

RasDJ

Jul 18 '05 #1
3 3426
On Sun, 30 Jan 2005 19:42:22 -0800, rasdj wrote:
I have a lot of SQL to convert to postgres from oracle. I have most of the
problems worked out except for this last bit. Many of my tables need the
last comma replaced with a close parenthesis - they look like this:

create table schema.table (
FLD000 NUMERIC(10,0) NOT NULL,
FLD001 CHAR(3) NOT NULL,
FLD002 DATE NOT NULL,
;

when the syntax requires:

FLD002 DATE NOT NULL)
;

I output the text in reverse thinking I could find the semicolon, go to
the next line and replace the 'comma newline' with 'closeparen newline'
and then go on to find the next semicolon.


You don't give a heck of a lot of details here, but the first thing that
leaps to mind is,

* Suck it all into a string, let's call it "s".
* s = s.replace(",\n;", ")\n;")
* Dump out s.

Failing that, regex can be used to allow for any whitespace, but worry
about that if this isn't enough. We can also discuss trying to stream this
operation if your SQL won't fit into memory all at once, but on modern
machines that would be a breathtaking number of table definitions that
would make me think you have other, larger problems :-)

Jul 18 '05 #2
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

Jul 18 '05 #3
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.

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kiteman - Canada | last post by:
I have the following: a form called Form1 an image called Image1 twelve lines that have been drawn and superimposed on the image I wish to have a command button that will toggle the lines on...
19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
2
by: Bengt Richter | last post by:
Is this a well known bug that's been fixed? I couldn't find any discussion of it, but maybe my googling's off today ;-/ >>> def foo(): ... it = iter(range(10)) ... while True: ... ...
1
by: Rocketman | last post by:
I am trying to find out when i reach the next line when multiline is switched on and i DON'T PRESS ENTER TO GO TO THE NEXT LINE(lines method is there for that purpose). Is there any other method i...
11
by: Neo Geshel | last post by:
I have an Access DB, from which I am going to pull images. Each image has an associated ID, but the ID's are not necessarily sequential (some images may have been deleted, leaving gaps in the list...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
7
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
6
by: Jacob Rael | last post by:
Hello, I have a simple script to parse a text file (a visual basic program) and convert key parts to tcl. Since I am only working on specific sections and I need it quick, I decided not to...
22
by: ashkaan57 | last post by:
Hi, I am trying to put text on left and right side of the page and used: <div> <span>blah blah</span> <span style="float:right">blah blah</span> </div> The 2nd text does go to the right but the...
0
by: Semajthewise | last post by:
Hi all. I'm starting on my next part of my teach myself vb program. What I am trying to do is on button click open a textfile showing the math as it would be done long hand. I started writing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.