3
$\begingroup$

I recently came across this behaviour of Import:

Export["new file.dat", {123,456,789}];
Import["new file.dat"]
Import["new*file.dat"]

The resulting Import has an additional level of braces if a wildcard is present in the Import instruction:

{{123}, {456}, {789}}
{{{123}, {456}, {789}}}

Although surprising, I'm guessing that the wildcard changes the heuristics Import uses to guess the format it's reading in. My question is, can an option be given to Import so that it produces the same output for both the wildcard and exact file specification? Preferably two options can be found to consistently switch between the two output.

I've tried specifying "Table" or "Data" formats to Import but the result stays different. Additionally, maybe someone knows how to find out which format Import chooses when no option is specified. I know the result can be post-processed to be consistent, but I think that giving instructions to Import would allow more flexibility in entering the file name.

This is on Mathematica 11.3 for Linux.

$\endgroup$
  • $\begingroup$ By the way, the difference also exists if the written data is a string. No wildcard gives a naked string and wildcard gives a string in a list. $\endgroup$ – Musang May 17 '18 at 14:40
  • $\begingroup$ If that helps, I can reproduce what you see on Mathematica 11 for MacOs $\endgroup$ – Fraccalo May 17 '18 at 14:45
  • 5
    $\begingroup$ The issue is not the guessing of the format. The issue is that since your file name is a pattern, it could match multiple file names, so Import returns a list of all matches it found (1 in your case). Not returning a list would be more inconsistent, because the same file name specification would then give differently structured results depending on the amount of files it found $\endgroup$ – Lukas Lang May 17 '18 at 14:47
  • $\begingroup$ @Mathe172 Is this documented anywhere? $\endgroup$ – Szabolcs May 17 '18 at 15:16
  • $\begingroup$ @Mathe172 That makes a lot of sense to me now that I've tested it out. Care to expand it into an answer? I can write it up if not. In this case it seems to me that there's no getting around processing the Import output after the fact. $\endgroup$ – Musang May 17 '18 at 15:22
7
$\begingroup$

Putting my comment into an answer, as requested.

There seems to be an undocumented feature of Import regarding wildcard support (if someone finds a reference, please leave a comment):

If the file name contains wildcards (string patterns do not seem to work), Import effectively maps over the result of FileNames, giving a list of results:

SetDirectory@CreateDirectory[];

Export["test1.txt", 1];
Export["test2.txt", 2];

Import["test1.txt"]
Import["test*.txt"]
(* "1" *)
(* {"1", "2"} *)

This explains what is happening in the question:

Import["*test1.txt"]
(* {"1"} *)

Even though there is only one file matched, Import still returns a list of all files found.

Workaround

The following should provide a robust workaround in case this behavior is undesired and it is clear that the wildcard pattern will always match exactly one file:

Import@First@FileNames["*test1.txt"]
(* "1" *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.