User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2007
Posts: 19
Reputation: LanierWexford is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Newbie Poster

Noob needs help i/o file data

  #1  
Nov 2nd, 2007
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

  1. fName=open('H:/CSC/Programs/CobolTran/Empcobol.dat')
  2. WriteToo=open('H:/CSC/Employee.dat','w')
  3. Trash='Start'
  4.  
  5. while Trash != EOF
  6. ID=fName.read(5)
  7. FullName=fName.read(26)
  8. Territory=fName.read(2)
  9. Office=fName.read(2)
  10. Salary=fName.read(6)
  11. SSN=fName.read(9)
  12. Dep=fName.read(2)
  13. JClass=fName.read(2)
  14. Trash=fName.readline()
  15. WriteToo.write(ID ','FullName','Territory',')
  16.  
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 12
Reputation: davidjhay is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
davidjhay davidjhay is offline Offline
Newbie Poster

Re: Noob needs help i/o file data

  #2  
Nov 2nd, 2007
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.

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
Reply With Quote  
Join Date: Dec 2006
Posts: 468
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 65
woooee woooee is offline Offline
Posting Pro in Training

Re: Noob needs help i/o file data

  #3  
Nov 2nd, 2007
You can also combine them and Python's garbage collector will then automatically close the file for you
for line in open(fname, "r"): ## One line at a time
## or
data = open(fname, "r").readlines()
for line in data:
Reply With Quote  
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Noob needs help i/o file data

  #4  
Nov 2nd, 2007
Actually the whole exercise is a great example of Python's slicing operator ...
  1. # slicing operator seq[begin : end(exclusive) : step]
  2. # step is optional
  3. # defaults are index begin=0, index end=len(seq), step=1
  4. """
  5. separate info in line into
  6. ID#,Last,First,Territory#,Office#,Salary,SSN,Department#,JobClass
  7. 00001,ADAMSON,JAMES,01,01,40000.00,145503242,01,02
  8. """
  9.  
  10. line = "00001JAMES ADAMSON 010104000014550324201021500067500040010011593"
  11. # extract the id using slicing
  12. id = line[0:5]
  13. # test
  14. print id, type(id)
  15.  
  16. # slice out the id and extract name and rest of data
  17. # have to do it this way, because firstname and lastname is not
  18. # of fixed length, but terminated with a space character
  19. no_id = line[5:].split()
  20. first = no_id[0]
  21. last = no_id[1]
  22. rest = no_id[2]
  23. # test
  24. print first, last, rest
  25.  
  26. # assume that next 6 data items are fixed in length
  27. territory = rest[0:2]
  28. office = rest[2:4]
  29. salary = "%0.2f" % float(rest[4:10])
  30. print territory, office, salary, type(salary)
  31. # keep going ...
  32. ssn = rest[10:19]
  33. department = rest[19:21]
  34. jobclass = rest[21:23]
  35. # test
  36. print ssn, department, jobclass
  37. """
  38. my output so far -->
  39. 00001 <type 'str'>
  40. JAMES ADAMSON 010104000014550324201021500067500040010011593
  41. 01 01 40000.00 <type 'str'>
  42. 145503242 01 02
  43. """
  44.  
  45. # the 9 extracted data variables look ok
  46. # now form a comma separated string
  47. format = "%s,%s,%s,%s,%s,%s,%s,%s,%s"
  48. data = format % (id,first,last,territory,office,salary,ssn,department,jobclass)
  49. # test it
  50. print data
  51. """
  52. my output -->
  53. 00001,JAMES,ADAMSON,01,01,40000.00,145503242,01,02
  54. """
May 'the Google' be with you!
Reply With Quote  
Join Date: Sep 2007
Posts: 19
Reputation: LanierWexford is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
LanierWexford LanierWexford is offline Offline
Newbie Poster

Re: Noob needs help i/o file data

  #5  
Nov 2nd, 2007
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
Reply With Quote  
Join Date: Dec 2006
Posts: 468
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 65
woooee woooee is offline Offline
Posting Pro in Training

Re: Noob needs help i/o file data

  #6  
Nov 2nd, 2007
# 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()
This will work for the entire file. If you mean that you would want, say the first 100 lines, the while loop would be
while (line) and (ctr < 100):
Last edited by woooee : Nov 2nd, 2007 at 5:44 pm.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,529
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Noob needs help i/o file data

  #7  
Nov 3rd, 2007
Originally Posted by LanierWexford View Post
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
Like woooee already pointed out, Python makes that very easy for you ...
  1. # convert a COBOL data file to a csv file
  2.  
  3. def cobol2csv(line):
  4. """
  5. separate COBOL type info in line
  6. eg. 00001JAMES ADAMSON 010104000014550324201021500067500040010011593
  7. to a csv string
  8. ID#,Last,First,Territory#,Office#,Salary,SSN,Department#,JobClass
  9. eg. 00001,ADAMSON,JAMES,01,01,40000.00,145503242,01,02
  10. """
  11. # extract the id using slicing
  12. id = line[0:5]
  13. # slice out the id and extract name and rest of data
  14. # have to do it this way, because firstname and lastname is not
  15. # of fixed length, but terminated with a space character
  16. no_id = line[5:].split()
  17. first = no_id[0]
  18. last = no_id[1]
  19. rest = no_id[2]
  20. # assume that next 6 data items are fixed in length
  21. territory = rest[0:2]
  22. office = rest[2:4]
  23. salary = "%0.2f" % float(rest[4:10])
  24. ssn = rest[10:19]
  25. department = rest[19:21]
  26. jobclass = rest[21:23]
  27. # now form a comma separated string and return it
  28. format = "%s,%s,%s,%s,%s,%s,%s,%s,%s"
  29. data = format % (id,first,last,territory,office,salary,ssn,department,jobclass)
  30. return data
  31.  
  32. #infile = open(r'H:/CSC/Programs/CobolTran/Empcobol.dat')
  33. #outfile = open(r'H:/CSC/Employee.dat','w')
  34. # for test only ...
  35. infile = open("empcobol.dat","r")
  36. outfile = open("empcsv.dat", "w")
  37.  
  38. for cobol_line in infile:
  39. csv_line = cobol2csv(cobol_line) + '\n' # add a new line char
  40. outfile.write(csv_line)
  41. # test
  42. print cobol_line
  43. print csv_line
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC