Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem writing to a file each record read

Reply
Thread Tools

problem writing to a file each record read

 
 
Eduardo Biano
Guest
Posts: n/a
 
      03-15-2006

I am a python newbie and I have a problem with writing
each record read to a file. The expected output is 10
rows of records, but the actual output of the code
below is only one row with a very long record (10
records are lump into one record). Thank you in
advance for your help. Here is the code:

****************************************

infile = open('c:/grad3650txt.csv', 'r')
outfile = open('c:/idmast01.txt', 'w')

a = 1
b = 10
for line in infile.readlines():
if a <= b:
c1 = line.find(',',0)
ln = line[1:c1]
l2 = line.find('"', c1)
l2a = l2 - 1
l2c = l2
if line[l2a] == '.':
l2b = l2 - 3
if line[l2b] == ' ':
mi = line[l2b+1:l2a+1]
l2c = l2b
else:
mi = ' '
l2c = l2
fn = line[c1+2:l2c]
c2 = line.find(',', l2)
c3 = line.find(',', c2+1)
deg = line[c2+1:c3]
l5 = len(line)
c4 = line.find(',', c3+1)
l7 = c3 + 14
if l5 <= l7 or c4 <= l5:
l8 = l5
if c4 <= l5:
dat = line[c3+1:c4]

#-----------------------------------
# This is the problem code. Ten records
# is lump into 1 row and written
# in the output file.
#
# My expected output is 10 rows.
#-----------------------------------
info = string.join([fn, mi, ln, deg, dat]

outfile.write(info)
#----------------------------------
#
a = 1 + a
else:
a = 1 + a

infile.close()
outfile.close()
# ------------------------------------------------

Thank you very much for your help.





__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-15-2006
Eduardo Biano wrote:

> #-----------------------------------
> #**This*is*the*problem*code.*Ten*records
> #**is**lump*into*1*row*and*written
> #**in*the*output*file.
> #
> #**My*expected*output*is*10*rows.
> #-----------------------------------
> info*=*string.join([fn,*mi,*ln,*deg,*dat]


Should probably be ",".join([fn,*mi,*ln,*deg,*dat])

> outfile.write(info)


You have to add a newline explicitly if you want one:

outfile.write("\n")

Using the csv module that comes with Python may simplify your task a lot.

Peter
 
Reply With Quote
 
 
 
 
bruno at modulix
Guest
Posts: n/a
 
      03-15-2006
Eduardo Biano wrote:
> I am a python newbie and I have a problem with writing
> each record read to a file. The expected output is 10
> rows of records, but the actual output of the code
> below is only one row with a very long record (10
> records are lump into one record). Thank you in
> advance for your help. Here is the code:
>
> ****************************************
>
> infile = open('c:/grad3650txt.csv', 'r')


Use the csv module. It's in the standard lib. And it'll save you lot of
wasted time and aspirin.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file line by line and write each line to a file based on the5th byte scad C++ 23 05-17-2009 06:11 PM
Problem writing a Faster_csv generated record to a file thiel Ruby 2 02-11-2009 04:20 PM
xsl:for-each for each 3 elements problem Tjerk Wolterink XML 3 11-03-2004 05:22 PM
Record Numbers against record problem... David ASP General 3 08-04-2004 02:43 PM
Need to concatenate all files in a dir together into one file and read the first 225 characters from each file into another file. Tony Perl Misc 5 04-19-2004 03:28 PM



Advertisments