1
$\begingroup$

I've received a data file in a mysterious ".pos" format. It consists of many lists that are separated by the space character; these are in turn separated by commas,e.g., { {a b c},{d e f},...} Apparently this file can be imported but can not fully exploited. For example, filename[[2]] can extract {d e f}, but neither Take, Drop or Part work to extract a particular value of each, or any, space delimited list. How can such values be extracted?

Additionally, I've not been able to Google any information of ".pos" file formats. Is this format known to Mathematica/Wolfram Language?

$\endgroup$
  • $\begingroup$ Probably because they are imported as strings. Try ToExpression[filename] $\endgroup$ – Rohit Namjoshi Jan 11 at 3:59
  • $\begingroup$ I also thought so, but StringQ[filename] was False. ".pos" files must have some sort of Delimiter Spaced Values (DSV) format. Thanks anyway. $\endgroup$ – stqnt Jan 11 at 13:14
2
$\begingroup$

I'd like to point out that there is the import/export format "Table" that can be customized to address different kinds of delimiters. Here is an import example that treats spaces as field separators and commas as line separators:

Import["file.pos", "Table",
 "FieldSeparators" -> " ",
 "LineSeparators" -> ","
 ]

If your data is really stored in the format {{a b c},{d e f},...}, it should be possible to be imported with the following:

Developer`ToPackedArray[
  Import["file.pos", "Table", "FieldSeparators" -> " ", 
    "LineSeparators" -> {"{", "},{", "}"}][[3 ;; -3]]
  ]

Unfortunately, I cannot test this because you have not provided any example file. BTW., I assumed that the data in the file is numeric; even that is not clear from your problem description.

$\endgroup$
  • $\begingroup$ Import::erropts: The value {"{","}"} specified for the option "CurrencyTokens" is invalid. $\endgroup$ – Somos Jan 12 at 9:48
  • 1
    $\begingroup$ Okay, I stress it once more: We need an example file or at least an example string from that file here. $\endgroup$ – Henrik Schumacher Jan 12 at 9:58
  • $\begingroup$ Yes, the data is numeric, and yes, Import["filename.pos", "Table"] worked. $\endgroup$ – stqnt Jan 12 at 16:36
0
$\begingroup$

The following worked: Import["filename.pos", "Table"] worked.

$\endgroup$
0
$\begingroup$

The following code should be close to what you want:

StringSplit[ StringReplace[ Import[filename, "CSV"][[1]],
   {"{" -> "", "}" -> ""}]] // ToExpression

You may need to tweak it if it does not do exactly what you want. An example of use is

stringfile = "{{a b c},{d e f},{g}}"; 
StringSplit[ StringReplace[ ImportString[stringfile, "CSV"][[1]],
    {"{" -> "", "}" -> ""}]] // ToExpression

Yielding the result

{{a, b, c}, {d, e, f}, {g}}
$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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