13、IDL读取txt为数组及注意事项

1.打开txt

从影像中读取数据保存为txt,直接就可以使用了

PRO opentxt

  COMPILE_OPT idl2
  envi, /restore_base_save_files
  envi_batch_init, log_file='batch.txt'


  txtname="C:\Users\HP\Desktop\a.txt"
  if file_test(txtname)then begin;The FILE_TEST function checks files for existence
  ;and other attributes without having to first open the file/用来检查文件是否存在及在不打开文件的情况下检查其属性
    nLines=file_lines(txtname);file_lines()用于返回数据的行
    tmp='';两个单引号
    
;  打开文件
    openr,lun,txtname,/get_lun
    while(~EOF(lun))do begin;
      readf,lun,tmp;读取第一行                  
;      print,tmp
;      help,tmp
      Col=strsplit(tmp,/extract);拆分第一行获取列数
      ColNum=N_elements(Col)
      Arr_1=fltarr(ColNum,nLines-1);定义一个n列n-1行,元素为0的数组
      readf,lun,Arr_1
      Arr_1=[[Col],[Arr_1]];重新组合数组(n,n-1)+第一行=(n,n)      
    endwhile
    print,Arr_1[0:1,*]
    help,Arr_1[0:1,*]
  end
  Arr_2=Arr_1[2:*,*]
  print,Arr_2
  help,Arr_2
END

2.注意事项

1. 无法读取为数组的情况

如果txt中的某行或某列中存在与其他行、列存在不一致的元素个数,那么该txt是无法读取为数组的

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值