PDA

View Full Version : Python: Reading From Resource



Sailor5
November 27th, 2010, 11:52 PM
Ahoy Sailors!

So I'm developing some Python built software intended for public use. The user will input some preferences which are saved in the Py2Exe built exe as a text resource. Now we're just needed to get the executable to find in itself the stored preferences.

The stored data is in between some tags
<0161ENTEREDDATA0161>The way I've tried is to get the file to open a handle to itself and use find() to look for these tags and attempt to extract the data. For some unknown reason the script I made using this method didn't work.

Can anyone give an example of how to read text from a resource in a file?

Thanks bye!

mo.reina
November 28th, 2010, 01:08 AM
what are you trying to do? extract whatever is between the tags?

Sailor5
November 28th, 2010, 02:23 AM
what are you trying to do? extract whatever is between the tags?

Yes. Which is stored in the file as a text resource.

debsankha
November 28th, 2010, 09:30 AM
Ahoy Sailors!

So I'm developing some Python built software intended for public use. The user will input some preferences which are saved in the Py2Exe built exe as a text resource. Now we're just needed to get the executable to find in itself the stored preferences.

The stored data is in between some tags
<0161ENTEREDDATA0161>The way I've tried is to get the file to open a handle to itself and use find() to look for these tags and attempt to extract the data. For some unknown reason the script I made using this method didn't work.

Can anyone give an example of how to read text from a resource in a file?

Thanks bye!

This will do what you want:


import re
re.findall("<0161(.*?)0161>",your_string)

DaithiF
November 28th, 2010, 11:38 AM
@Sailor5, your approach seems a little strange to me ... you want to store user preferences within a python script that you're compiling into an exe ... why are you compiling it into an exe, and why wouldn't you use a (separate) config text file?

I ask because your approach seems to me like maybe you're coming from a windows c/c++ programming background where you have resource bundles compiled into exe files. That isn't typical for python though ... maybe you have good reason for doing it this way but it just seems strange to me.