Re: File Import
- To: mathgroup at smc.vnet.net
- Subject: [mg32582] Re: [mg32530] File Import
- From: Dale Horton <daleh at wolfram.com>
- Date: Wed, 30 Jan 2002 03:18:57 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
At 01:58 AM 1/25/2002, Yas wrote:
>Dera Mathgroup,
>I have a bunch of files with name pattern, abX_pattern1_pattern2.txt. I
>would like to Import many of these files, operate on them and then save
>the output to another file.
>
>1. How do I go about writing something in Mathematica which will import
>the files by recognising the patterns. In the above pattern1 is an
>integer and pattern2 contains a numbers and the letter o (e.g pattern2 =
>0o5 or 0o1) automatically. An example from my notebook is as follows,
>w8 = Import["ttX_1000_0o5.txt", "Table"];
This will recognize anything for the two patterns:
fns = FileNames["*X_*_*.txt"]
Map[Import[#, "Table"]&, fns]
If you want to make sure that the * are of the type that you want, then you
need to do some more complicated tests.
testfileQ[str_] :=
Module[{chars=Characters[str], pattern},
pattern = {_, _, "X", "_", p1___?DigitQ, "_", x___?DigitQ, "o",
y___?DigitQ, ".", "t", "x", "t"};
MatchQ[chars, pattern]
]
Then, you can use
fns = Select[FileNames[], testfileQ]
>2. After this I operate on the data which produces an output of two
>numbers l, and m. I want to write these output values to a file as
>columns of l, m, pattern1, pattern2, with pattern 2 converted to a
>number ( in the example above, 0o5 to 0.5 say), then move on to the next
>file and so forth.
infofromfiles[str_] :=
Module[{chars=Characters[str], pattern, pat1, pat2, i, m},
pattern = {_, _, "X", "_", p1___?DigitQ, "_", x___?DigitQ, "o",
y___?DigitQ, ".", "t", "x", "t"};
{pat1, pat2} = chars/.pattern :> {StringJoin[p1], StringJoin[x, ".", y]};
data = Import[str, "Table"];
(* You need to define this function *)
{i, m} = MyOperationOnTheData[data];
{i,m, pat1, pat2}
]
fns = Select[FileNames[], testfileQ];
out = infofromfiles/@fns;
Export["results.txt", out, "Table"]
>Any help will be appreciated.
>
>Thanks
>Yas
-Dale