MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Data Conversion

  • To: mathgroup at smc.vnet.net
  • Subject: [mg38022] Re: [mg38006] Data Conversion
  • From: "Y.A.Tesiram" <yas at pcomm.hfi.unimelb.edu.au>
  • Date: Tue, 26 Nov 2002 00:48:52 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On Mon, 25 Nov 2002 Moranresearch at aol.com wrote:

>
> I have the following data of the following form.
>
> {123-456-7899, John Smith,2223334444}
>
> How can I convert this to
> {123,456,7899, John,Smith,222,333,4444}
> and then to
>
> {1234567899johnsmith00000)
>
> 1. The "-" are removed
> 2. John Smith is concerted to lower case
> 3. The space between John and Smith is removed (ie john and smith is
> concatenated)
> 4. 123454567899johnsmith is padded right so the the total  number of
> characters is 24.
> Thank you.
> John
>
>


G'day John,


Hi, This will do what you want and you may want to modify it to be more
robust.

Some notes and function foobar[file_String] follows;

1. Import the file as Word(s), not String
2. Replace the hyphen first to avoid confusion with minus
3. WRT l3 in foobar, first step takes 222333444 and inserts a comma and
space in the 4th and 7th positions. Second step takes the remaining
elements and makes a list. Then convert to string and then to expression.
The space after the comma just got inserted as one character I think and
you can't do much with that.
4. WRT to l4, get rid of the last three entries in the expression and add
the zeroes, convert to string and replace the unwanted characters with
wanted characters.


In[1]:=

foobar[file_String] := Module[{l1, l2, l3, l4},
    l1 = ReadList[file, Word, RecordSeparators -> {","}];
    l2 = StringReplace[l1, {"-" -> ","}];
    l3 = {Drop[l2, -1], StringInsert[Last[l2], ", ", {4, 7}]} // ToString
//ToExpression // Flatten;
    l4 = StringReplace[{Drop[l3, -3], {0, 0, 0, 0, 0}} // Flatten //
          ToString, {"," -> "", " " -> "", "J" -> "j", "S" -> "s"}]
    ]


In[2]:=

foobar["~/Desktop/data.txt"]

Out[2]=

{1234567899johnsmith00000}


Cheers
Yas




  • Prev by Date: Re: Mathematica Documentation
  • Next by Date: Re: Setting Options Back to Default Values? (and other graphics queries)
  • Previous by thread: Re: Data Conversion
  • Next by thread: Re: Data Conversion