当前位置:网站首页>ENVI_ Idl: reading of text files (mainly txt and CSV files)
ENVI_ Idl: reading of text files (mainly txt and CSV files)
2022-07-19 02:10:00 【Fried eggplant】
1. The functions you need to use
——————————————————————————————————————————
——————————————————————————————————————————
openr, 1, route ——》1 It refers to the memory address of the file under this path
Variable = file_lines( route )——》 The path where the incoming file is located , Return the number of lines of the file
readf, Substitute for , Variables receiving data ——》 The first parameter is a substitute for the memory address of the file , The second is the variable that receives the data
skip_lun, Substitute for , Row number , /lines——》 Specify the nickname of the file , Specify the number of lines to skip , /lines Indicates that the line skipped before
free_lun, 1 Substitute for ——》 Specify the nickname of the file , Memory usage for closing files
——————————————————————————————————————————
——————————————————————————————————————————
2. Programming
pro week_six_study1
; How to read text data ( Mainly read txt、csv Suffix text file )(txt Are separated by spaces ,csv Are separated by half width commas )
; route
file_path = 'D:/IDL_program/experiment_data/chapter_4/2013_year_aop.txt'
; Xiaobai reads txt file
; Open the txt file ——》 If you just read the file openr That's all right. ,openw You can read or write ( Will overwrite the original file , Use with caution )
openr, 1, file_path ; first 1 Used to represent the memory address of this file , The second is the path of the file
; Because the first row is the index , The index is text , Below the index are numbers , The two types are inconsistent , It cannot be stored in an array , So we need to skip
skip_lun, 1, 1, /lines ; first 1 It refers to the number of the file or the memory address , the second 1 Indicates the number of lines to skip , Only the first line is skipped here , So it is 1, The third indicates that the unit of the second parameter is row, not column
; Create a stored array
box_data = fltarr(5, 56304) ; Open the file through notepad and check the number of rows and columns of the data in the file in advance ( share 5 Column 56305 That's ok , But skip the first line, so just 56304 That's ok )
; Get the data of the file
readf, 1, box_data
; Check it out.
print, box_data[0, -1] ; Find a new thing is idl The value of can also be negative ( and python equally , I don't know ), This is equivalent to box_data[0, 56303]
; Close file ( Develop good habits )
free_lun, 1
; Normal people read txt file
; Open the txt file
openr, 1, file_path ; If you don't have free_lun,1 Then it can't be opened here
; Get the index data in the first row , This is also useful ( Get the number of columns ), Skipping seems a little wasteful
; There is one in advance str Variables store
str = ''
readf, 1, str ; Here you may wonder , But what? ?readf It's about giving as much as you have , Now? str Is a string , The default output is one line , If you are an array , Then, according to the number of rows and columns of the array, give as many
; For the obtained data with the first row of index stored str Variables for analysis , Analyze how many columns the whole data has through this string
son_str_array = strsplit(str, /extract) ; Do not pass in what is the separator , By default, space is used as the separator of the string
; son_str_array There are several elements in it , Then the whole data has several columns
column = n_elements(son_str_array)
; Now get the number of rows
row = file_lines(file_path) - 1 ; The first line is the index , We won't put it in the following array
; Create a stored array
box_data = fltarr(column, row)
; get data ( The number of lines obtained here starts from the beginning of the second line , Because it's already readf, 1, str 了 , So the file pointer has pointed to the beginning of the second line )
readf, 1, box_data
; Check it out.
print, box_data[0, -1]
; Close file
free_lun, 1
; Master read txt file
; Open file
openr, 1, file_path
; Skip the first line
skip_lun, 1, 1, /lines
; The number of lines to read from the file
row = file_lines(file_path) - 1
; Create a stored list
box_data = fltarr(5, row) ; there 5 It's through txt Open notepad and see
; obtain txt File data
readf, 1, box_data
; Check it out.
print, box_data[0, -1]
; Close file
free_lun, 1
; Maybe you'll ask , What's the advanced , I want to say , Advanced is not everything that needs programming , Efficiency is the most important
; In this document , We know that the number of columns generally does not change , So why not open it directly with Notepad , But the number of lines is different for each file
; It's ok if it's a file , If there are many documents , Do you open it with Notepad , It's unrealistic , I won't talk about more .
; Read csv file
; route
file_path = 'D:/IDL_program/experiment_data/chapter_4/air_quality_data.csv'
; Read the file
data = read_csv(file_path, header=par_name) ; The index of the first row is given to the data par_name( But whether you need the first row of index data or not ,read_csv() The function will not read the data in the first row by default )
; see data The type of
help, data ; You can find , This is a structure
; Output the first column of data
print, data.(0)
; Output the index data of the first row
print, par_name
; The output column index is NO2 The data of ( In the first method, you can count the column number by yourself, read it and output it in the above way , You can also use the following method )
NO2_index = where(par_name eq 'NO2')
print, data.(NO2_index)
end———————————————————————————————————————————
———————————————————————————————————————————
I am fried eggplant , Thank you. !!
边栏推荐
- Frustratingly Simple Few-Shot Object Detection
- close 和 shutdown区别
- Fair Multiple Decision Making Through Soft Interventions
- Saber's most powerful digital analog mixed signal simulation software
- 基于蒙特卡洛的强化学习方法【附带代码实现】
- DGC最佳实践:机密数据入湖,如何保证数据不被泄露?
- SAE J1708/J1587 协议详解
- ENVI_IDL:批量处理Modis Swath数据的重投影并输出为Geotiff格式+详细解析
- Problems encountered in yolov3 training its own data set
- [cute new problem solving] sum of three numbers
猜你喜欢
随机推荐
gdb+vscode进行调试4——gdb执行相关命令
Fairness in Deep Learning: A Computational Perspective
Set up sqoop environment
指针常量与常量指针爱恨情仇
笔记一之IDL基础内容:常用数据类型_创建数组_类型转换_print输出_基本运算_关系运算
成信大ENVI_IDL第二周课后作业:提取n个点的气溶胶厚度+详细解析
03 design of urban road dedusting and cooling system based on ZigBee
The differences and usage of cookies, localstorage and sessionstorage
Build map reduce development environment
IGBT 直通短路过程问题分析
基于蒙特卡洛的强化学习方法【附带代码实现】
Recursive and recursive learning notes
DGC最佳实践:机密数据入湖,如何保证数据不被泄露?
Owl Eyes: Spotting UI Display Issues via Visual Understanding
搭建Hue环境
04基于ZigBee的室内无线定位系统设计
Foo bar what the hell?
搭建Sqoop环境
在Oozie中配置 map-reduce workflow
YYDS!阿里技术官最新总结的分布式核心技术笔记已上线,堪称福音









