[ncl-talk] Sub:Read CSV

Rashed Mahmood rashidcomsis at gmail.com
Mon Jan 8 13:54:40 MST 2018


Hi Dale,

I do not how your file looks like, so created a dummy file without any
header lines (attached).
Following two methods should work for what you want:

mehtod1:

 nrow   = 10
 ncol    = 25
 infile1 = "dummy_file.csv"
 lines1  = asciiread (infile1, -1, "string")
 delim   = ","
 data    = new((/nrow,ncol/),float)
 do n=0,ncol-1
    tmp       = tofloat(str_get_field(lines1,2+n,delim)) ; .......... note
str_get_field starts at 1, NOT 0
    data(:,n) = tmp(0:nrow-1)
 end do
 print(data(:,0)+","+data(:,1)+","+data(:,2))


method2 (suggested by Mary):

 nrow   = 10
 ncol    = 25

 infile1 = "dummy_file.csv"
 lines1  = asciiread (infile1, -1, "string")
 delim   = ","
 csv_lines = str_split_csv(lines1,delim,0)
 data      = csv_lines(0:nrow-1,1:ncol-1)
 print(data(:,0)+","+data(:,1)+","+data(:,2))


Hope that helps, and let me know if you have further questions regarding
this.

Rashed






On Sun, Jan 7, 2018 at 7:28 PM, dale zuri via ncl-talk <ncl-talk at ucar.edu>
wrote:

> Hi ,
> Thanks.  I would like to extract specific row by specific column.
> For example, leaving the date column extracting the first 10 rows and 25
> columns.
> I couldn't leave the date column. Could someone help me to figure it out?
>
> Dz
>
>
> On Tue, Nov 28, 2017 at 7:44 AM Mary Haley <haley at ucar.edu> wrote:
>
>> Hi,
>>
>> For CSV data, I recommend using str_split_csv to extract the data:
>>
>> infile1 = "AK_BackCast_StnData-Temp.csv"
>> lines1  = asciiread (infile1, -1, "string")
>> delim = ","
>>
>> ;months starts in column #0
>>
>> csv_lines = str_split_csv(lines1(2:),delim,0)
>> date      = csv_lines(:,0)          ; first column
>> col2      = toint(csv_lines(:,1))   ; second column
>> col3      = toint(csv_lines(:,2))   ; third column
>>
>> print(col3)    ; use "tofloat" in place of "toint" if you want floating
>> point data
>>
>> --Mary
>>
>>
>> On Mon, Nov 27, 2017 at 4:25 PM, dale zuri <dalezuri at gmail.com> wrote:
>>
>>> Hi ,
>>> I have an issue with reading the CSV.
>>>
>>> I would appreciate any help and suggestions.
>>>
>>> Thanks
>>> begin
>>>
>>>
>>> infile1 = "AK_BackCast_StnData-Temp.csv"
>>> lines1  = asciiread (infile1, -1, "string")
>>> print(lines1)
>>> delim = ","
>>> ;months starts in field number 1
>>> name  =  str_get_field(lines1,3,delim)
>>> print(name)
>>>
>>> I want to read each column with associated string field.
>>> Except number 1 and 2, the rest of the field numbers are not correct.
>>> For example, number 3 field, print the different column.
>>>
>>> I don't understand whats happening here.
>>>
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> ncl-talk at ucar.edu
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>>
>>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180108/a4027334/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dummy_file.csv
Type: text/csv
Size: 4319 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180108/a4027334/attachment.csv>


More information about the ncl-talk mailing list