Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old January 5th, 2004, 02:55 AM
evod510 evod510 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 evod510 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 5th, 2004, 07:54 AM
ragabash ragabash is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Yuma,AZ
Posts: 11 ragabash User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to ragabash Send a message via Yahoo to ragabash
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

Reply With Quote
  #3  
Old January 5th, 2004, 06:44 PM
evod510 evod510 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 evod510 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

thanks, I will try it asap.

Reply With Quote
  #4  
Old January 5th, 2004, 11:55 PM
evod510 evod510 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 evod510 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old January 6th, 2004, 02:12 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #6  
Old January 6th, 2004, 03:24 AM
evod510 evod510 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 evod510 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old January 6th, 2004, 04:58 AM
ragabash ragabash is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Yuma,AZ
Posts: 11 ragabash User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to ragabash Send a message via Yahoo to ragabash
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.

Reply With Quote
  #8  
Old January 6th, 2004, 07:10 PM
evod510 evod510 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 evod510 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #9  
Old January 7th, 2004, 02:35 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #10  
Old January 7th, 2004, 07:37 PM
evod510 evod510 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 6 evod510 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #11  
Old January 7th, 2004, 09:57 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
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

Reply With Quote
  #12  
Old January 8th, 2004, 02:35 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
There are actually a few nice examples of looping/iterating over dictionaries in this thread..

http://forums.devshed.com/showthrea...threadid=100130

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > String Conversion


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |