By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

Parse file into array

P: n/a
I was wondering how i could parse the contents of a file into an array.
the file would look something like this:

gif:image/gif
html:text/html
jpg:image/jpeg
....

As you can see, it contains the mime type and the file extension
seperated by commas, 1 per line. I was wondering if it was possible to
create and array like this:

(Pseudocode)
mimetypearray[gif] = "image/gif"
mimetypearray[html] = "text/html"
mimetypearray[jpg] = "image/jpeg"
....

I come from a PHP backround where I know this is possible, but I am new
at Python. Please disregard this if it is a stupid question.

Nov 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> I was wondering how i could parse the contents of a file into an array.
the file would look something like this:

gif:image/gif
html:text/html
jpg:image/jpeg


Try something like this:

d = {}
for line in open("input.txt").readlines():
ext, mime = line.strip().split(":")
d[ext] = mime
print d

Craig
Nov 22 '05 #2

P: n/a
amfr wrote:
I was wondering how i could parse the contents of a file into an array.
the file would look something like this:

gif:image/gif
html:text/html
jpg:image/jpeg
...

As you can see, it contains the mime type and the file extension
seperated by commas, 1 per line. I was wondering if it was possible to
create and array like this:

(Pseudocode)
mimetypearray[gif] = "image/gif"
mimetypearray[html] = "text/html"
mimetypearray[jpg] = "image/jpeg"
...


You want a dictionary, not an array.

mimetypedict = {}
for line in mimetypefile:
line = line.rsplit('\r\n')
extension, mimetype = line.split(':')
mimetypedict[extension] = mimetype

Note that there's already a MIME type database in the standard mimtypes
module: <http://python.org/doc/current/lib/module-mimetypes.html>.
Nov 22 '05 #3

P: n/a
Thanks a lot. The webserver I am writing works now :)

Nov 22 '05 #4

P: n/a
Leif K-Brooks wrote:
line = line.rsplit('\r\n')

Er, that should be line.rstrip, not line.rsplit.
Nov 22 '05 #5

P: n/a
If it helps, there's a builtin module for figuring out mimetypes;

http://docs.python.org/lib/module-mimetypes.html
import mimetypes
mimetypes.guess_type('.gif')

('image/gif', None)
Cheers,

Andy.

Nov 22 '05 #6

P: n/a
On 14 Nov 2005 13:48:45 -0800, "amfr" <am******@gmail.com> wrote:
I was wondering how i could parse the contents of a file into an array.
the file would look something like this:

gif:image/gif
html:text/html
jpg:image/jpeg
...

As you can see, it contains the mime type and the file extension
seperated by commas, 1 per line. I was wondering if it was possible to
create and array like this:

(Pseudocode)
mimetypearray[gif] = "image/gif"
mimetypearray[html] = "text/html"
mimetypearray[jpg] = "image/jpeg"
...

I come from a PHP backround where I know this is possible, but I am new
at Python. Please disregard this if it is a stupid question.

Pretty much anything is possible in Python, if you can conceive it well enough ;-)

Assuming f is from f = open(yourfile), simulated with StringIO file object here,
from StringIO import StringIO
f = StringIO("""\ ... gif:image/gif
... html:text/html
... jpg:image/jpeg
... """)

And assuming that there are no spaces around the ':' or in the two pieces,
but maybe some optional whitespace at either end of a line and \n at the end
(except maybe the last line), and no blank lines, you can get a dict mapping easily:
mimetypedict = dict(tuple(line.strip().split(':')) for line in f)
This gives you: mimetypedict {'gif': 'image/gif', 'html': 'text/html', 'jpg': 'image/jpeg'}

Which you can access using the names as keys, e.g., mimetypedict['gif'] 'image/gif'

If you want to use bare names to access the info via an object, you can use the
dict info to create a class or class instance and give it the named attributes, e.g.
a class with the data as class variables is quick:
MTC = type('MTC',(), mimetypedict)
MTC.gif 'image/gif' MTC.jpg

'image/jpeg'

Or you could substitute the mimetypedict expression from above to make another one-liner ;-)

Other ways of setting up your info are certainly possible, and may be more suitable,
depending on how you intend to use the info. As mentioned, the mimetypes module
may already have much of the data and/or functionality you want.

Regards,
Bengt Richter
Nov 22 '05 #7

P: n/a
On 14 Nov 2005 13:48:45 -0800, "amfr" <am******@gmail.com> wrote:
I was wondering how i could parse the contents of a file into an array.
the file would look something like this:

gif:image/gif
html:text/html
jpg:image/jpeg
...

As you can see, it contains the mime type and the file extension
seperated by commas, 1 per line. I was wondering if it was possible to
create and array like this:

(Pseudocode)
mimetypearray[gif] = "image/gif"
mimetypearray[html] = "text/html"
mimetypearray[jpg] = "image/jpeg"
...

I come from a PHP backround where I know this is possible, but I am new
at Python. Please disregard this if it is a stupid question.

Pretty much anything is possible in Python, if you can conceive it well enough ;-)

Assuming f is from f = open(yourfile), simulated with StringIO file object here,
from StringIO import StringIO
f = StringIO("""\ ... gif:image/gif
... html:text/html
... jpg:image/jpeg
... """)

And assuming that there are no spaces around the ':' or in the two pieces,
but maybe some optional whitespace at either end of a line and \n at the end
(except maybe the last line), and no blank lines, you can get a dict mapping easily:
mimetypedict = dict(tuple(line.strip().split(':')) for line in f)
This gives you: mimetypedict {'gif': 'image/gif', 'html': 'text/html', 'jpg': 'image/jpeg'}

Which you can access using the names as keys, e.g., mimetypedict['gif'] 'image/gif'

If you want to use bare names to access the info via an object, you can use the
dict info to create a class or class instance and give it the named attributes, e.g.
a class with the data as class variables is quick:
MTC = type('MTC',(), mimetypedict)
MTC.gif 'image/gif' MTC.jpg

'image/jpeg'

Or you could substitute the mimetypedict expression from above to make another one-liner ;-)

Other ways of setting up your info are certainly possible, and may be more suitable,
depending on how you intend to use the info. As mentioned, the mimetypes module
may already have much of the data and/or functionality you want.

Regards,
Bengt Richter
Nov 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.