I have a text file with a set of replacement rules, of the form

x^3y^2 -> 4, 
xy^2 -> 2, 

for example. I'm having some issues trying to figure out how to tell Mathematica to read those rules and apply them. So far I've tried

F[x,y] /. "!cat rules.txt" 

but that gave an invalid syntax error. Any suggestions?


After some initial suggestions, here is some extra information:

After using the Import command as suggested by rm -rf, and removing the last comma from the text file, mathematica does not interpret the result as a list of replacement rules.

If I now do

replaceList = {Import["rules.txt"]};
F[x3,x4,x5] /. replaceList

Mathematica now says:

ReplaceAll::reps: {x5^3->5,
x2*x5^2->5} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>


Ok all, here is a very concrete example of what I'm facing. Thank you for helping me thus far, I'm sorry to be vague. In a text file called rules.txt I have the following content:

$ cat rules.text 

In mathematica I have the following code followed with an error:

F[x1_, x2_, x3_] := x1 *x2^2 + x1^2*x3 + x1^3
rules = Import["rules.text"]
{{"x1*x2^2->1,"}, {"x1^2*x3->3,"}, {"x1^3->4"}}

Obviously the formatting of the rules are incorrect, but if we try to brute force it, we get the error:

F[x1, x2, x3] /. rules
ReplaceAll::reps: {x1*x2^2->1,} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
(*followed by similar errors for the other replacement rules read in*)

One quick attempt at fixing is to flatten the import table, or do what Michael E2 suggested:

rules = Flatten[Import["rules.text"]]
out = {"x1*x2^2->1,", "x1^2*x3,", "x1^3->4"}

F[x1, x2, x3] /. rules
"\!\({\"x1*x2^2->1,\", \"x1^2*x3->3,\", \"x1^3->4\"}\) is neither a list \
of replacement rules nor a valid dispatch.."


rules = ToExpression@Import["rules.text"]
ToExpression::sntx: Invalid syntax in or before "x1*x2^2->1, ".
ToExpression::sntx: Invalid syntax in or before "x1^2*x3->3, ".

Which fails also. Please let me know if I can add any more information to make the issue clearer, and thanks again.


I found a workaround solution. If one removes the comma from the text file, so that it reads:

$ cat rules.dat 

the following commands resolve the issue:

F[x1_, x2_, x3_] := x1 *x2^2 + x1^2*x3 + x1^3
F[x1, x2, x3] /. ToExpression@Flatten[Import["rules.dat"]]
out = 8

note the importance of the .dat extension. .txt does not work.

  • $\begingroup$ I'm curious — why did you think "!cat rules.txt" would do what you wanted it to do? Have you looked at Import? $\endgroup$
    – rm -rf
    Nov 18, 2013 at 2:22
  • $\begingroup$ Is the rule data you show a verbatim sample of your text file? Does each line end in a comma? Is there a black line between every rule? $\endgroup$
    – m_goldberg
    Nov 18, 2013 at 2:26
  • $\begingroup$ @m_goldberg You could bet there isn't. $\endgroup$ Nov 18, 2013 at 2:45
  • $\begingroup$ @rm-rf: I just looked at import, I think this is what I was looking for, thank you-- I still have an issue, see my other comment I add shortly. $\endgroup$
    – Richard
    Nov 18, 2013 at 2:50
  • $\begingroup$ @belisarius. I sure you're right. I meant to type "blank line". $\endgroup$
    – m_goldberg
    Nov 18, 2013 at 2:50

2 Answers 2


Here's a way to deal with the original text file:

"{" <> Import[FileNameJoin[{$TemporaryDirectory, "rules.text"}], 
   "String"] <> "}" // ToExpression

(* {x1 x2^2 -> 1, x1^2 x3 -> 3, x1^3 -> 4} *)

How did your rules come to be in a text file? I'd recommend storing them in one of two ways:

rules = {x^3 y^2 -> 4, x y^2 -> 2};
rules >> rules.dat

then e.g

x y^2 /. Get["rules.dat"]

To me this is an intuitive Mathematica way of storing your rules. Unfortunately you cannot do this with CDF, not even when using Player Pro, because Put fails (bizarre undocumented trap for Player Pro and CDF Enterprise users). So for Player Pro deployment I have gone with a different option:

Export["rules.txt", Compress[rules]]

then when you need the rules:

  • $\begingroup$ Hi Mike, thanks a lot for your answer. Unfortunately the issue with my interfacing it in this way is that the replacement rules are coming from another program written in Sage by another group of people working on a similar research problem. I had no choice but to try and import from a text file, but then again I'm glad it's figured out and someone in the future could refer to this (ugly) method and move on! $\endgroup$
    – Richard
    Nov 18, 2013 at 4:17

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.