Function Repository Resource:

RegisterFormat

Source Notebook

Register an extension or MIME type for automatic detection in Import, Export, and FileFormat

Contributed by: Sean Cheren

ResourceFunction["RegisterFormat"]["fmt", <|"Extensions"ext|>]

registers the extension ext to format "fmt".

ResourceFunction["RegisterFormat"]["fmt", <|"MIMETypes"type|>]

registers the mime type to format "fmt".

ResourceFunction["RegisterFormat"]["fmt", <|"Extensions"ext,"MIMETypes"mime|>]

registers the extension ext and the mime type format "fmt".

Details and Options

Both ext and type can be a string, or list of strings where the first item in the list is the default.
Once an extension is registered, FileFormat, Import, and Export will recognize the extension automatically.
ResourceFunction["RegisterFormat"] works in conjunction with ImportExport`RegisterImport and ImportExport`RegisterExport to add custom formats to Import, ImportString, Export, etc. See Developing an Import Converter and Developing an Export Converter for details on how to register a format.
The format will only stay registered for the kernel session.
There is no way to undo a format registration in a kernel session.

Examples

Basic Examples (2) 

Register "myjson" to the "JSON" format:

In[1]:=
ResourceFunction["RegisterFormat"]["JSON", <|"Extensions" -> "myjson"|>]
Out[1]=

Now "myjson" will additionally be recognized as an extension for "JSON":

In[2]:=
Export["test.myjson", 1];
FileFormat["test.myjson"]
Out[3]=

Register format "myFmt":

In[4]:=
myFormatImport[filename_String, opts : OptionsPattern[]] := StringDelete[Import[filename, "String"], "myFormat!\n"];
ImportExport`RegisterImport["myFmt", myFormatImport];
myFormatExport[filename_String, expr_, opts : OptionsPattern[]] := Export[filename, "myFormat!\n" <> expr, "String"];
ImportExport`RegisterExport["myFmt", myFormatExport];

Register an extension for "myFmt":

In[5]:=
ResourceFunction["RegisterFormat"]["myFmt", <|"Extensions" -> "myext"|>]
Out[5]=

Import, Export, and FileFormat, now infer the extension from the file name:

In[6]:=
Export["test.myext", "Hello, world!"]
Out[6]=
In[7]:=
FileFormat["test.myext"]
Out[7]=
In[8]:=
FilePrint["test.myext"]
In[9]:=
Import["test.myext"]
Out[9]=

Scope (6) 

RegisterFormat can also be used to modify the MIME type or extension of an existing format. Create a test file with a custom extension "mycsv" to register to "CSV":

In[10]:=
Export["test.csv", 1];
Export["test.mycsv", 1, "CSV"];

The extension .csv should already be recognized:

In[11]:=
FileFormat["test.csv"]
Out[11]=

By default, "mycsv" would be imported at "Text":

In[12]:=
FileFormat["test.mycsv"]
Out[12]=

Register "mycsv" to the "CSV" format:

In[13]:=
ResourceFunction["RegisterFormat"]["CSV", <|"Extensions" -> "mycsv"|>]
Out[13]=

Now "mycsv" will additionally be recognized as an extension for "CSV":

In[14]:=
FileFormat["test.mycsv"]
Out[14]=

The original extensions still work:

In[15]:=
FileFormat["test.csv"]
Out[15]=

Version History

  • 1.0.0 – 25 October 2019

Related Resources

Author Notes

In 12.0 the mime type registration is mostly pointless, it can only be accessed via Import["file.txt","MIMEType"].
In 12.1 it will be used by Import when importing from a URL, and be accessible with new utility functions: ImportExport`FormatName, ImportExport`FormatToMIMEType (List), ImportExport`FormatToExtension (List), ImportExport`MIMETypeToFormat (List), ImportExport`ExtensionToFormat (List) will all work with these.

License Information