Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2013

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

Search the Archive

Re: Import CSV can not understand string correctly

  • To: mathgroup at smc.vnet.net
  • Subject: [mg131715] Re: Import CSV can not understand string correctly
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Mon, 23 Sep 2013 21:58:10 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net
  • References: <20130921083610.2A1EC6A34@smc.vnet.net>

data = Import["/Users/hanlonr/Desktop/test.csv", "Table"];


data2 = (StringSplit[#[[1]], ","] & /@ data) /.
  x_String?(AtomQ[ToExpression[#]] &) :> ToExpression[x]


{{1, 2, "3D22", 4, 5, 6, "7D63"}, {8, 9, "5D61", 1.234, 5.678, 8, "9D45"}}


Map[Head, data2, {2}]


{{Integer, Integer, String, Integer, Integer, Integer, String}, {Integer,
  Integer, String, Real, Real, Integer, String}}



Bob Hanlon




On Sun, Sep 22, 2013 at 6:35 AM, Joug Raw <jougraw at gmail.com> wrote:

> Thanks for your answered. I have just tested it. It works in the sense of
> 3D22 can be stored in string.
>
> However, there are three main problems:
>
> 1- The whole line(recored) in the .csv file is stored in a single string
> in mma. All the fields separated by comma in .csv file are merged into one
> string in mathematica.
>
> 2- the comma are written together with two double quote sign in the string
> in Mathematica, which is "\",\"". So, the double quote is a real string and not
> a symbol.
>
> 3- numbers in the .csv file are transformed into strings and belong to
> part of the line strings in Mathematica.
>
> These together make the data very difficult to process. Then there is no
> difference for me to use ReadList rather then Import to read the file and
> then add many StringReplace or ReplacePart or ReplaceAll and so on to
> do complicated text processing.
>
> Since Import["filename", CSV] works fine for everything except the
> misunderstanding of specific string format. Is there any way to still use
> it and just disable transform 3D22 like string to number?
>
>
> On Sat, Sep 21, 2013 at 4:32 PM, Bob Hanlon <hanlonr357 at gmail.com> wrote:
>
>> This works for me:
>>
>> data = Import["filepathname", "Table"]
>>
>>
>>
>> Bob Hanlon
>>
>>
>>
>>
>> On Sat, Sep 21, 2013 at 4:36 AM, Joug Raw <jougraw at gmail.com> wrote:
>>
>>>
>>> In my CSV file, I have string fields like 3D22 or so.
>>> While I imported the CSV file in mathemtaica, the 3D22 string was
>>> converted
>>> to number 3x10^22.
>>>
>>> How dose this happened??? How can I avoid this kind of transformation and
>>> keep my original string when Import a CSV file?
>>>
>>>
>>>
>>
>




  • Prev by Date: Data set association, returning values
  • Next by Date: Re: list mutability (very basic question)
  • Previous by thread: Re: Import CSV can not understand string correctly
  • Next by thread: Re: Import CSV can not understand string correctly