The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
String Conversion
Discuss String Conversion in the Python Programming forum on Dev Shed. String Conversion Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 5th, 2004, 02:55 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation 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
|

January 5th, 2004, 07:54 AM
|
|
Junior Member
|
|
Join Date: Aug 2003
Location: Yuma,AZ
Posts: 11
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Re: String Conversion
Quote: 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
|

January 5th, 2004, 06:44 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
thanks, I will try it asap. 
|

January 5th, 2004, 11:55 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation 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 
|

January 6th, 2004, 02:12 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

January 6th, 2004, 03:24 AM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation 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])
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
|

January 6th, 2004, 04:58 AM
|
|
Junior Member
|
|
Join Date: Aug 2003
Location: Yuma,AZ
Posts: 11
Time spent in forums: < 1 sec
Reputation 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:
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.
|

January 6th, 2004, 07:10 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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
|

January 7th, 2004, 02:35 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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...000000000000000
Take care,
Mark.
|

January 7th, 2004, 07:37 PM
|
|
Junior Member
|
|
Join Date: Jan 2004
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
tq for the link
but i found the example a bit confusing
any other link for directory specially with looping condition
tq in advance 
|

January 7th, 2004, 09:57 PM
|
|
Contributing User
|
|
Join Date: Mar 2002
Posts: 89
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 12
|
|
|

January 8th, 2004, 02:35 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|