|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Re: String Conversion
Quote:
Make a dictionary: codeLookUp = {'1234': 'Apple', '5678':'Orange'} newfile.write(codeLookUp[code]) -rag |
|
#3
|
|||
|
|||
|
thanks, I will try it asap.
![]() |
|
#4
|
|||
|
|||
|
Dear ragabash,
something wrong with the coding i think here is the error message Code:
newfile.write(codeLookUp[code]) KeyError: 1635 help please ![]() |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
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 |
|
#9
|
||||
|
||||
|
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. |
|
#10
|
|||
|
|||
|
tq for the link
but i found the example a bit confusing any other link for directory specially with looping condition tq in advance ![]() |
|
#11
|
|||
|
|||
|
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 |
|
#12
|
||||
|
||||
|
There are actually a few nice examples of looping/iterating over dictionaries in this thread..
http://forums.devshed.com/showthrea...threadid=100130 Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > String Conversion |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|