File Input output in Mathematica?

You asked to read (every line,one at a time) Well, that is surely not the Mathematica way of doing things, but as you asked for it, try something along the lines of.

You asked to read (every line,one at a time). Well, that is surely not the Mathematica way of doing things, but as you asked for it, try something along the lines of: strInp = OpenRead "Test. In"; strOut = OpenWrite "Test.

Out"; While(str=ReadstrInp, Number)! = EndOfFile, out = yourprocessstr; Write strOut,out; ; Close strOut; Close strInp; (* Now show the output file *) FilePrint "Test. Out Edit Other answers posted better and more Mathematica-ish ways of doing this, but that generally implies NOT reading one at a time, as Mathematica favors functional, list-wide programming rather than the iterative way.

Single nit: you do not clean up if you Abort. You need to wrap it in CheckAbort, otherwise there is a chance the file will remain open. – rcollyer Mar 14 at 3:21 1 @rcollyer I always try to fly away from this kind of processing in Mma.

The file management is just not robust enough. I saw corrupt files and checkpoint management is very difficult to implement. I guess the better path is not to make this approach robust, but to switch to the most natural one (all file in one glup).

– belisarius Mar 14 at 3:29 sometimes you can't, so I have a utility function (stackoverflow. Com/questions/4174791/…) that wraps the file processing with CheckAbort for me, called either Execute Around Code or RAII pattern. Saves my bacon.

I usually don't do writes, but I've got the code generalized that it can do either. Although, with the writes there is no guarantee as to the state of the file if I abort midstream, so to speak. – rcollyer Mar 14 at 4:46.

It's rather clunky to read each value one at a time, but it's natural in M- to read them all at once and then process each one. Here's a simple infrastructure I use all the time: (* step one: get data *) data = Import"ideone_fM0rs. Txt", "Lines"; (* step two:?

*) res = {}; Module{value, result}, value = #; result = yourCodeHerevalue; AppendTores, result; & /@ data; (* step three: PROFIT! *) Export"out. Txt", res, "Lines"; or a variation with Sow/Reap for the times you'd like to collect values under various tags or categories or genuses or whatever: data = Import"ideone_fM0rs.

Txt", "Lines"; res = ReapModule{value, result, tag}, value = #; result = yourCodeHerevalue; tag = generateTagvalue Sowresult, tag; & /@ data, _, Rule2; Export"out. Txt", res, "Lines"; It's tempting to roll all that up into a single awe-inspiring one-liner, but for maintainability I like to keep it unrolled with each step made explicit. Of course, yourCodeHerevalue could instead be many lines of well-commented code.

Note: I downloaded your data to a local file ideone_fM0rs. Txt using the download link at http://ideone.com/fM0rs.

1 "It's tempting to roll all that up into a single awe-inspiring one-liner" isn't it always... – acl Mar 13 at 12:22.

That's straightforward to do, and like everything in Mathematica there is more than one way to do it. Personally, I'd use data = ReadList"Test. In", Number, RecordLists-> True; and then process data using Map.

There's also Import and your data probably is best loaded as type Table, although you can check the full list and see what's there. You could also use Read, but you'd have to control the file open/close yourself.

For the input aspect, this might give you a start. Vals = Import"ideone.com/fM0rs", "Table" /. {aa_ /;!

NumberQaa && FreeQaa, List, ___} :> Sequence /. {} :> Sequence I think others on this forum may have better ways to go about it though; I'm not much versed in the Import/Export realm. Daniel Lichtblau Wolfram Research.

The returned Length@vals is 397, and the OP posted a file with 200 records. You are reading the "line numbers" too, but that should give a length of 400 :( – belisarius Mar 13 at 2:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions