•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 456,580 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,616 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 1224 | Replies: 6
![]() |
•
•
Join Date: Sep 2007
Posts: 19
Reputation:
Rep Power: 2
Solved Threads: 0
I am trying to learn Python by converting my C++ homework to Python. Below I am converting a Cobol file of employees to a comma delimited file.
from:
00001JAMES ADAMSON 010104000014550324201021500067500040010011593
to:
ID#,Last,First,Territory#,Office#,Salary,SSN,Department#,JobClass
00001,ADAMSON,JAMES,01,01,40000.00,145503242,01,02
I want to use EOF as my while loop control but I am getting a syntax error. Am I heading down the right path? Is there a better way to write this code? Any help would be great! Also this is my first posting about code, so if my format can be improved in any way, help here would be great too. Thanx in advance!
Lanier
from:
00001JAMES ADAMSON 010104000014550324201021500067500040010011593
to:
ID#,Last,First,Territory#,Office#,Salary,SSN,Department#,JobClass
00001,ADAMSON,JAMES,01,01,40000.00,145503242,01,02
I want to use EOF as my while loop control but I am getting a syntax error. Am I heading down the right path? Is there a better way to write this code? Any help would be great! Also this is my first posting about code, so if my format can be improved in any way, help here would be great too. Thanx in advance!
Lanier
python Syntax (Toggle Plain Text)
fName=open('H:/CSC/Programs/CobolTran/Empcobol.dat') WriteToo=open('H:/CSC/Employee.dat','w') Trash='Start' while Trash != EOF ID=fName.read(5) FullName=fName.read(26) Territory=fName.read(2) Office=fName.read(2) Salary=fName.read(6) SSN=fName.read(9) Dep=fName.read(2) JClass=fName.read(2) Trash=fName.readline() WriteToo.write(ID ','FullName','Territory',')
•
•
Join Date: Mar 2007
Posts: 12
Reputation:
Rep Power: 2
Solved Threads: 1
Lanier,
When using file i/o commands I use the following method. I am not an expert though so there may be a better way.
Hope this helps
David
When using file i/o commands I use the following method. I am not an expert though so there may be a better way.
file = open('file.txt','r')
data = file.readlines()
while data:
fname = data[0][5:10] #reads first line 5th to 10th characters
data = data[1:] #chops of first line of data.Hope this helps
David
Actually the whole exercise is a great example of Python's slicing operator ...
python Syntax (Toggle Plain Text)
# slicing operator seq[begin : end(exclusive) : step] # step is optional # defaults are index begin=0, index end=len(seq), step=1 """ separate info in line into ID#,Last,First,Territory#,Office#,Salary,SSN,Department#,JobClass 00001,ADAMSON,JAMES,01,01,40000.00,145503242,01,02 """ line = "00001JAMES ADAMSON 010104000014550324201021500067500040010011593" # extract the id using slicing id = line[0:5] # test print id, type(id) # slice out the id and extract name and rest of data # have to do it this way, because firstname and lastname is not # of fixed length, but terminated with a space character no_id = line[5:].split() first = no_id[0] last = no_id[1] rest = no_id[2] # test print first, last, rest # assume that next 6 data items are fixed in length territory = rest[0:2] office = rest[2:4] salary = "%0.2f" % float(rest[4:10]) print territory, office, salary, type(salary) # keep going ... ssn = rest[10:19] department = rest[19:21] jobclass = rest[21:23] # test print ssn, department, jobclass """ my output so far --> 00001 <type 'str'> JAMES ADAMSON 010104000014550324201021500067500040010011593 01 01 40000.00 <type 'str'> 145503242 01 02 """ # the 9 extracted data variables look ok # now form a comma separated string format = "%s,%s,%s,%s,%s,%s,%s,%s,%s" data = format % (id,first,last,territory,office,salary,ssn,department,jobclass) # test it print data """ my output --> 00001,JAMES,ADAMSON,01,01,40000.00,145503242,01,02 """
May 'the Google' be with you!
•
•
Join Date: Dec 2006
Posts: 468
Reputation:
Rep Power: 2
Solved Threads: 65
# You can use a for loop
for line in open(fname, "r"): ## One line at a time
print line
#
# or a while loop
fp=open(fname, "r")
line = fp.readline()
while line:
print line
line = fp.readline()
fp.close()while (line) and (ctr < 100):
Last edited by woooee : Nov 2nd, 2007 at 5:44 pm.
•
•
•
•
Vega,
Thanx for the lesson in slicing. The process make so much sense now. The only thing I am unsure of is how would I run a while loop to slice an unknown number of lines from the file?
Lanier
python Syntax (Toggle Plain Text)
# convert a COBOL data file to a csv file def cobol2csv(line): """ separate COBOL type info in line eg. 00001JAMES ADAMSON 010104000014550324201021500067500040010011593 to a csv string ID#,Last,First,Territory#,Office#,Salary,SSN,Department#,JobClass eg. 00001,ADAMSON,JAMES,01,01,40000.00,145503242,01,02 """ # extract the id using slicing id = line[0:5] # slice out the id and extract name and rest of data # have to do it this way, because firstname and lastname is not # of fixed length, but terminated with a space character no_id = line[5:].split() first = no_id[0] last = no_id[1] rest = no_id[2] # assume that next 6 data items are fixed in length territory = rest[0:2] office = rest[2:4] salary = "%0.2f" % float(rest[4:10]) ssn = rest[10:19] department = rest[19:21] jobclass = rest[21:23] # now form a comma separated string and return it format = "%s,%s,%s,%s,%s,%s,%s,%s,%s" data = format % (id,first,last,territory,office,salary,ssn,department,jobclass) return data #infile = open(r'H:/CSC/Programs/CobolTran/Empcobol.dat') #outfile = open(r'H:/CSC/Employee.dat','w') # for test only ... infile = open("empcobol.dat","r") outfile = open("empcsv.dat", "w") for cobol_line in infile: csv_line = cobol2csv(cobol_line) + '\n' # add a new line char outfile.write(csv_line) # test print cobol_line print csv_line
May 'the Google' be with you!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- how to read data from text file into data grid??? (VB.NET)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- Cannot copy file: Data error (cylcic redundancy check) (Windows NT / 2000 / XP / 2003)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
- Cannot copy file: Data error (cylcic redundancy check) (Windows NT / 2000 / XP / 2003)
- Help creating a data file (C)
Other Threads in the Python Forum
- Previous Thread: Palindrome Help
- Next Thread: def save(): print "HOW DO I DO THIS?"



Linear Mode