12

Has anyone implemented the import of YAML data files into Mathematica? I suppose some sort of transformation into a nested list of rules would be the way to do it. Thanks

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
John Jowett
  • 599
  • 2
  • 9

3 Answers3

6

I wrote such a cross-platform tool to convert other formats to wolfram format: WolframExchange

Usage

wex in.yml      # check file but no output
wex in.yml -t   # output only text
wex in.yml -c   # output only binary
wex in.yml -ct  # output both text and binary

Result

enter image description here

enter image description here

Aster
  • 3,836
  • 1
  • 18
  • 44
5

I just wrote an importer: https://github.com/zbjornson/MYaml. It's built on SnakeYAML and supports all language-independent types.

Example 2.10 from the spec:

In[1]:= ImportString["---
 hr:
   - Mark McGwire
   # Following node labeled SS
   - &SS Sammy Sosa
 rbi:
   - *SS # Subsequent occurrence
   - Ken Griffey", "YAML"]
Out[1]= {
          "hr" -> {"Mark McGwire", "Sammy Sosa"}, 
          "rbi" -> {"Sammy Sosa", "Ken Griffey"}
        }

More examples in the test suite.

ZachB
  • 1,200
  • 9
  • 19
  • 2
    For demonstration purposes, can you show how your importer acts on a sample YAML file? – J. M.'s missing motivation Oct 12 '16 at 03:21
  • @J.M. good call, added an example. – ZachB Oct 12 '16 at 05:42
  • 1
    Consider posting it on http://packagedata.net/ too – Szabolcs Oct 12 '16 at 06:41
  • Another importer was written just a few days ago, see at the end of comment thread here: http://mathematica.stackexchange.com/q/127300/12 – Szabolcs Oct 12 '16 at 06:43
  • Added to packagedata, thanks. :) – ZachB Oct 12 '16 at 17:35
  • I got a successful "Installed YAML importer to..." and restarted the kernel, but if I try to use it with Import or ImportString I get the error "The Import element "YAML" is not present when importing as Text". I believe this is because it's not recognizing the "YAML" as a data format and is falling back and assuming it's a specified element of the data. Anyway to troubleshoot this? – Jess Riedel Sep 05 '18 at 23:49
  • 1
    @JessRiedel You're correct. I'm not on a computer with M right now but can double check in the morning that a fresh install works. What version of M are you using? You can try manually loading this file to register the importer, something like << FileNameJoin[{$BaseDirectory, "SystemFiles", "Formats", "YAML", "Import.m"}]. – ZachB Sep 06 '18 at 02:21
  • Gah, it turns out I just needed to use $UserBaseDirectory instead of $BaseDirectory. The folder at $BaseDirectory doesn't actually exist on my machine, and the installation code fails silently in this case (which is totally reasonable given the audience size; I should have just checked things more carefully). Thanks much for your fast help. – Jess Riedel Sep 06 '18 at 02:48
  • Thanks for reporting back. I'll try to make it print a helpful error in that situation. – ZachB Sep 06 '18 at 03:59
2

If you cannot find a YAML importer, consider converting the YAML to JSON and importing that. It'll give you the list of rules you'd expect.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263