Re: Trouble related to "CSV"
- To: mathgroup at smc.vnet.net
- Subject: [mg90521] Re: Trouble related to "CSV"
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 11 Jul 2008 02:06:31 -0400 (EDT)
On 7/10/08 at 6:35 AM, email.slim at gmail.com wrote: >On Jul 9, 1:58 am, Bill Rowe <readn... at sbcglobal.net> wrote: >>On 7/8/08 at 2:26 AM, email.s... at gmail.com wrote: >>>I'm trying to read in data from a text file composed of strings >>>and tables of comma separated integers. >>Import["teststr.txt","CSV"] >>will do it for you? This will work in Mathematica version 4 and >>newer. >The problem is the text file is composed of mix of strings and >integers, some are separated by space, and the rest by commas. But >I'll definitely try =02> Import["teststr.txt","CSV"] and see how it >turns out. Import will deal with a mixed data types just fine. But none of the built in file reading commands will deal with mixed separators without some additional work on your part. In the case of Import, those items separated by a space likely will be treated as one string when using "CSV" as the format. Assuming there are no embedded spaces in any of the string data types, you can fix this by using StringReplace to replace all of the spaces with commas. You can read all rows of your file as a String using ReadLList["teststr.txt",String"] Once this is done, you can write out the corrected strings to a file that can be used with Import. As an alternative, you could use StringSplit to break the strings you read in using ReadList into fields then use ToExpression to convert the integer strings to integers. If there are no mixed data types in a given column, this likely is somewhat faster and not too cumbersome. But if the columns have mixed data types, writing the corrected strings to a new file and using Import is likely to be more efficient.