#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    6
    Rep Power
    0

    String Conversion


    Greetings,

    I need some help from any master out there,

    I need to convert a string of text from a files into a format that i can understand. Here is the coding that i did:
    Code:
    blocksize = 88
    
    origfile = open('source.DAT', 'rb')
    newfile = open('targer.TXT', 'wb')
    
    while 1:
    	datachunk = origfile.read(blocksize)
    	if not datachunk:
    		newfile.write('end')
    		break
    	rcd = datachunk[0:1] + ';'
    	no = datachunk[1:10] + ';'
    	type = datachunk[10:16] + ';'
    	date = datachunk[16:28] + ';'
    	ref = datachunk[28:40] + ';'
    	code = datachunk[40:52] + ';'
    	qty = datachunk[52:64] + ';'
    	tran1 = datachunk[64:76] + ';'
    	tran2 = datachunk[76:88] + ';'
    
    	newfile.write(rcd)
    	newfile.write(no)
    	newfile.write(type)
    	newfile.write(date)
    	newfile.write(ref)
    	newfile.write(code)
    	newfile.write(qty)
    	newfile.write(tran1)
    	newfile.write(tran2)
    	
    
    	newfile.write('\r\n')
    origfile.close()
    newfile.close()
    Now i need to convert the list of code into a list of name
    example
    if code == 1234 then write apple
    if code == 3456 then write orange
    etc (more than 50 items)

    can anyone guide me how to insert the coding
    thanks
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Yuma,AZ
    Posts
    11
    Rep Power
    0

    Re: String Conversion


    Originally posted by evod510

    Now i need to convert the list of code into a list of name
    example
    if code == 1234 then write apple
    if code == 3456 then write orange
    etc (more than 50 items)

    can anyone guide me how to insert the coding
    thanks
    Make a dictionary:

    codeLookUp = {'1234': 'Apple', '5678':'Orange'}
    newfile.write(codeLookUp[code])

    -rag
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    6
    Rep Power
    0

    Thumbs up


    thanks, I will try it asap.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    6
    Rep Power
    0
    Dear ragabash,

    something wrong with the coding i think

    here is the error message


    [CODE]
    newfile.write(codeLookUp
    Code:
    )
    KeyError: 1635
    help please
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You need to set up your dictionary (with keys) before hand or you'll end up with a KeyError, so your dictionary doesn't have a key 1635...

    Code:
    >>> codes = {'1': 'one', '2': 'two', '3': 'three'}
    >>> codes
    {'1': 'one', '3': 'three', '2': 'two'}
    >>> for key, value in codes.items():
    	print '%s => %s' % (key, value)
    
    	
    1 => one
    3 => three
    2 => two
    >>> for each in codes:
    	print each
    
    	
    1
    3
    2
    >>> codes['1']
    'one'
    >>> codes['2']
    'two'
    >>> codes['3']
    'three'
    >>> codes['4']
    
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in -toplevel-
        codes['4']
    KeyError: '4'
    >>>
    If you could post your new program, probably be very useful

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    6
    Rep Power
    0
    here is the script


    [CODE]

    blocksize = 88

    origfile = open('source.DAT', 'rb')
    newfile = open('targer.TXT', 'wb')

    while 1:
    datachunk = origfile.read(blocksize)
    if not datachunk:
    newfile.write('end')
    break
    rcd = datachunk[0:1] + ';'
    no = datachunk[1:10] + ';'
    type = datachunk[10:16] + ';'
    date = datachunk[16:28] + ';'
    ref = datachunk[28:40] + ';'
    code = datachunk[40:52] + ';'
    qty = datachunk[52:64] + ';'
    tran1 = datachunk[64:76] + ';'
    tran2 = datachunk[76:88] + ';'

    newfile.write(rcd)
    newfile.write(no)
    newfile.write(type)
    newfile.write(date)
    newfile.write(ref)
    codeLookUp = {'5924 ':'Ball','1015 ':'Pen','6351 ':'Ink','7083 ':'Ruler','5015 ':'Ashtray','7129 ':'Box','5076 ':'Clip')
    newfile.write(codeLookUp
    Code:
    )
    Code:
     
    	newfile.write(qty)
    	newfile.write(tran1)
    	newfile.write(tran2)
    	
    
    	newfile.write('\r\n')
    origfile.close()
    newfile.close()
    I know something wrong but my knowledge is very limited.

    Tq in advance
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Yuma,AZ
    Posts
    11
    Rep Power
    0
    Your slicing code into a 12 character string, right padded with spaces I'm guessing and adding a trailing ';' and then storing it into the variable code. So code acctually looks something like this:
    Code:
    '1234        ;'
    Which is what you want to use as your key if your going to look it up by that whole thing. Or, use:
    Code:
    newfile.write(codeLookUp[code[:6]])
    which will leave code the same, but take the first 6 characters to do a look up in your dictionary.

    -rag
    Last edited by ragabash; January 6th, 2004 at 05:01 AM.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    6
    Rep Power
    0

    Thumbs up


    tq

    i understand what you are saying

    kindly can you assist me in the dictionary function

    where can i find suitable source/link so i can understand it

    more

    tq again
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The dictionary is a type... like lists, strings and tuples! So , not a function, if anything you'd have to call it a class . I think thats an important point to get accross here since your gonna be working with OOP eventually.

    You can find all the stuff you need to know in the Python tutorial, but here's a link to one dictionary spacific section

    http://www.python.org/doc/2.3.3/tut/...00000000000000

    Take care,
    Mark.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    6
    Rep Power
    0

    Question


    tq for the link

    but i found the example a bit confusing

    any other link for directory specially with looping condition

    tq in advance
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    try reading the first part of this and it'll explain dictionaries http://www.ibiblio.org/obp/thinkCSpy/chap10.htm
    I'm not sure how much you'll understand the rest of it though
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There are actually a few nice examples of looping/iterating over dictionaries in this thread..

    using iterator on a dictionary

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo