By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Formatting question.

P: n/a
Hi all,

My input file looks like this : ( the data is separated by tabs )

11/26/2007 56.366 898.90 -10.086 23.11 1212.3
11/26/2007 52.25 897.6 -12.5 12.6 13333.5
11/26/2007 52.25 897.6 -12.5 12.6 133.5

The output I'm trying to get is as follows :

( Insert NBUSER.Grochamber Values
'11/26/2007','56.366','898.90','-10.086','23.11','1212.3', )
( Insert NBUSER.Grochamber Values
'11/26/2007','52.25','897.6','-12.5','12.6','13333.5', )
( Insert NBUSER.Grochamber Values
'11/26/2007','52.25','897.6','-12.5','12.6','133.5', )

The following is the program i have written so far :
LoL = []

for line in open('mydata.txt'):
LoL.append(line.split("\t"))

print "read from a file: ", LoL,

outfile = open("out.dat", "w")

lilength = len(LoL)
liwidelength = len(LoL[1])
print "length of list is " , lilength, "long"
print "length of list is " , liwidelength, "long"

for x in range(lilength):
outfile.write(" ( ")
outfile.write('Insert NBUSER.Grochamber Values ')
for y in range(liwidelength):
outfile.write( "'%s'," % (LoL[x][y]))
outfile.write(" ) \n")

outfile.close()
I have 3 questions :

1. The formatting in the first line comes out wrong all the time. I m
using windows python 2.5.1. The last part of the first line is always
on the second line.

2. How do I avoid the "," symbol after the last entry in the line?
(this are supposed to be sql-queries - importing excel based tabbed
data to sql database)

3. What do I do when the data is missing? Like missing data?

Thanks for all your help!

Mike

Nov 20 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hey Mike,
Welcome to Python!

About your first issue, just change the line
outfile.write( "'%s'," % (LoL[x][y]))
With
outfile.write( "'%s'," % (LoL[x][y][:-1]))

Why? Because when you do the line.split, you are including the '\n' at
the end, so a new line is created.

Now, what you are doing is not very pythonic (batteries are included
in python, so you could just use the CSV module). Also, the for x in
range(len(somelist)) is not recommended, you can just do something
like:

========================
import csv

infile = open("mydata.txt", "rb")
outfile = open("out.txt", "wb")

reader = csv.reader(infile, delimiter='\t')
writer = csv.writer(outfile, quotechar=None, delimiter = "\\")

for row in reader:
data = "'" + "', '".join(row) + "'"
base = " ( Insert NBUSER.Grochamber Values %s, )"
writer.writerow([base % data])

infile.close()
outfile.close()
========================
The above lines works like your program, writing exactly what you asked.
Again, all lists are iterable, you don't need to iterate an integer
from 1 to len(list). (isn't python wonderful?)

HTH,
Sergio
On Nov 20, 2007 6:11 PM, mike5160 <mi******@gmail.comwrote:
Hi all,

My input file looks like this : ( the data is separated by tabs )

11/26/2007 56.366 898.90 -10.086 23.11 1212.3
11/26/2007 52.25 897.6 -12.5 12.6 13333.5
11/26/2007 52.25 897.6 -12.5 12.6 133.5

The output I'm trying to get is as follows :

( Insert NBUSER.Grochamber Values
'11/26/2007','56.366','898.90','-10.086','23.11','1212.3', )
( Insert NBUSER.Grochamber Values
'11/26/2007','52.25','897.6','-12.5','12.6','13333.5', )
( Insert NBUSER.Grochamber Values
'11/26/2007','52.25','897.6','-12.5','12.6','133.5', )

The following is the program i have written so far :
LoL = []

for line in open('mydata.txt'):
LoL.append(line.split("\t"))

print "read from a file: ", LoL,

outfile = open("out.dat", "w")

lilength = len(LoL)
liwidelength = len(LoL[1])
print "length of list is " , lilength, "long"
print "length of list is " , liwidelength, "long"

for x in range(lilength):
outfile.write(" ( ")
outfile.write('Insert NBUSER.Grochamber Values ')
for y in range(liwidelength):
outfile.write( "'%s'," % (LoL[x][y]))
outfile.write(" ) \n")

outfile.close()
I have 3 questions :

1. The formatting in the first line comes out wrong all the time. I m
using windows python 2.5.1. The last part of the first line is always
on the second line.

2. How do I avoid the "," symbol after the last entry in the line?
(this are supposed to be sql-queries - importing excel based tabbed
data to sql database)

3. What do I do when the data is missing? Like missing data?

Thanks for all your help!

Mike

--
http://mail.python.org/mailman/listinfo/python-list
Nov 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.