Parse file into array

A

amfr

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.
 
C

Craig Marshall

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
 
L

Leif K-Brooks

amfr said:
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"
...[/QUOTE]

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>.
 
B

Bengt Richter

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.
[/QUOTE]
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,
 ... 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:

This gives you: {'gif': 'image/gif', 'html': 'text/html', 'jpg': 'image/jpeg'}

Which you can access using the names as keys, e.g.,[QUOTE][QUOTE][QUOTE]
>>> mimetypedict['gif'][/QUOTE][/QUOTE][/QUOTE]
 '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:
 '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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top