2
$\begingroup$

So I am having trouble because I am trying to do with Mathematica what is equivalent do "sscanf" with C basically.

I will have a text file with a number of specifications:

data1 data2 data3 ...

Some of these parameters are strings, while others are numbers. Basically I want scan the line of the file and put each of these fields into variables. What is the best way to do this in Mathematica. I have seen very confusing information online, some using Import, others fread and I am still not sure what would be the best way.

Thank you in advance!

$\endgroup$
2
  • 1
    $\begingroup$ You could start by trying Import with CSV format. CSV and see how it works. What is the field separation? space, tab, comma? $\endgroup$
    – Nasser
    Jan 2, 2023 at 8:39
  • $\begingroup$ Hi @Nasser! Thank you. As for the separation initially I was doing space, than changed to comma, but I am not sure what will be easier for Mathematica to do the interpretation (I am defining the format of the files myself so I can still change it). $\endgroup$ Jan 2, 2023 at 8:50

1 Answer 1

9
$\begingroup$

You may also check the ReadList function. For example considering following input (having an implicit structure of String, Number, String, Number):

abc 123 def 456
abcdef 123.123 defghj 456.456

you can read it like:

ReadList["input.txt", {Word, Number, Word, Number}]

and getting the result in form of a list of records {{"abc", 123, "def", 456}, {"abcdef", 123.123, "defghj", 456.456}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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