Parse file into array
Leif K-Brooks
eurleif at ecritters.biz
Mon Nov 14 17:21:03 EST 2005
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>.
More information about the Python-list
mailing list