473,396 Members | 1,814 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.

problem writing to a file each record read


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
Mar 15 '06 #1
2 1128
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
Mar 15 '06 #2
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 'o****@xiludom.gro'.split('@')])"
Mar 15 '06 #3

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

Similar topics

20
by: titi | last post by:
Question The road and traffic authority for a small country currently uses a system to store information about all 'currently' licensed drivers. A licensed driver has the following info stored...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
4
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: Thelonious Monk | last post by:
I have a problem where some data is being eliminated. The problem is that the data contains signed numeric fields (the low-order byte of a negative number uses the first 4 bits as a sign and...
22
by: b_r | last post by:
Hi, I'm trying to make a simple operation (insert into DB) in VB 2005 and SQL Server. The code is as follows: Dim sConnectionString As String = _ "Data...
8
by: Ritesh Raj Sarraf | last post by:
Hi, I think there is some lock problem. Let me show the code first import os import sys import string import tempfile
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.