tokenpocket官方苹果app下载安卓|jld

作者: tokenpocket官方苹果app下载安卓
2024-03-07 19:25:31

Julia导入和导出数据(CSV、TXT、JLD)_jld 格式-CSDN博客

>

Julia导入和导出数据(CSV、TXT、JLD)_jld 格式-CSDN博客

Julia导入和导出数据(CSV、TXT、JLD)

最新推荐文章于 2023-04-23 21:51:23 发布

YonminMa

最新推荐文章于 2023-04-23 21:51:23 发布

阅读量9.1k

收藏

13

点赞数

1

分类专栏:

Julia

文章标签:

julia

jld

csv

txt

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_44547562/article/details/106943811

版权

Julia

专栏收录该内容

4 篇文章

0 订阅

订阅专栏

文章目录

CSV格式导出到CSV文件导入CSV文件

TXT格式导出到TXT文件导入TXT文件

JLD格式导出到JLD文件导入JLD文件

CSV格式

导出到CSV文件

在操作之前我们首先要安装DataFrames包和CSV包

Pkg.add("DataFrames")

Pkg.add("CSV")

创建一个DataFrame格式的数据df

using DataFrames

df = DataFrame(year = [1,2,3,3,3,3,3,3,54,5],name=["h","we","as","aa","av","af","ab","sa","ea","as"])

将df保存到mydata.csv中

using CSV

CSV.write("mydata.csv", df)

导入CSV文件

df = CSV.read("mydata.csv")

print(typeof(df))

describe(df)

输出为

TXT格式

导出到TXT文件

f = open("mydata.txt", "w")

write(f, "Hello\n")

write(f, "Julia")

close(f)

导入TXT文件

f = open("mydata.txt", "r")

lines = readlines(f) # 将每一行的文本分开保存到一个数组里

for line in lines

println(line)

end

close(f)

输出为

Hello

Julia

JLD格式

JLD格式是Julia特有的文件格式,在使用之前我们要先下载JLD包和HDF5包

Pkg.add("JLD")

Pkg.add("HDF5")

导出到JLD文件

将数据保存到JLD格式的文件的代码为

using JLD

using HDF5

var_a = [1,2,3,4]

var_b = ["a","b","c","d"]

save("mydata.jld", "VAR_A", var_a, "VAR_B", var_b)

导入JLD文件

data = load("mydata.jld", "VAR_A")

println(data)

data = load("mydata.jld", "VAR_B")

println(data)

输出为

[1, 2, 3, 4]

["a", "b", "c", "d"]

优惠劵

YonminMa

关注

关注

1

点赞

13

收藏

觉得还不错?

一键收藏

知道了

0

评论

Julia导入和导出数据(CSV、TXT、JLD)

文章目录CSV格式导出到CSV文件导入CSV文件TXT格式导出到TXT文件导入TXT文件JLD格式导出到JLD文件导入JLD文件CSV格式导出到CSV文件在操作之前我们首先要安装DataFrames包和CSV包Pkg.add("DataFrames")Pkg.add("CSV")创建一个DataFrame格式的数据dfusing DataFramesdf = DataFrame(year = [1,2,3,3,3,3,3,3,54,5],name=["h","we","as","aa",

复制链接

扫一扫

专栏目录

CSV.jl:使用Julia编程语言处理CSV和其他定界文件的实用程序库

02-03

CSV.jl:使用Julia编程语言处理CSV和其他定界文件的实用程序库

RData.jl:从Julia中读取R数据文件

02-04

RData.jl:从Julia中读取R数据文件

参与评论

您还未登录,请先

登录

后发表或查看评论

JLD2.jl:采用纯Julia格式的HDF5兼容文件格式

02-03

JLD2.jl:采用纯Julia格式的HDF5兼容文件格式

9.java程序员必知必会类库之加密库

最新发布

wlyang666的博客

04-23

3867

密码学在计算机领域源远流长,应用广泛。当前每时每刻,每一个连接到互联网的终端,手机,电脑,iPad都会和互联网有无数次的数据交互,如果这些数据都是明文传输那将是难以想象的。为了保护用户隐私,防止重要数据被窃取,篡改,我们需要对我们的数据进行加密验签

java实现图片与base64转换

qishinihenhao的专栏

01-16

4685

如果你是一个软件开发,不论前端后端工程师,图片的处理你是肯定要会的,关于图片的Base64编码,你可能有点陌生,但是这是一个软件工程师应该要掌握的知识点,现在很多网友把图片与base64转换都做成了小工具如:http://www.yzcopen.com/img/imgbase64

今天我们就一起来看一下吧。

base64编码 是将数据用 64 个可打印的字符进行编码的方式,任何数据底层实现都是二...

01 Julia语言基础知识-01

qq_36639444的博客

09-01

638

简单julia语法第一弹

julia常用矩阵函数_Julia矩阵操作与保持年轻的秘诀

weixin_39704246的博客

12-21

275

自语:话说Julia是一个神奇的语言,语法简单,速度贼快,是吹牛装X的不二神器。记得一个物理学家说过,那些旧理论之所以消失,不是因为人们改变了看法,而是持那种看法的人死光了。同样的道理(同样在哪里???),以后Fortran或者其它旧式的语言之所以消失,不是因为大家都学习了新语言,而是使用他们的人死光了。为了证明自己还永远年轻,就用一些时髦的词汇,看bilibili,玩QQ空间,听《两只老虎爱跳舞...

Julia : HDF5、JLD2库、group、dataset

Julia & Rust & Python

02-25

921

JLD2库中,group和dataset是什么样的安排,取决于读、写的细节。

一、准备h5材料

using H5JL;

using DelimitedFiles;

using JLD2;

csv_file = "C:\\Users\\songroom\\Desktop\\000001.XSHE.csv";

jld_file = "C:\\Users\\songroom\\Desktop\\0000...

Julia语言读取CSV文件示例一

isS_MK_GmbH的博客

06-12

4394

使用的Julia版本为1.1.0

代码如下:

using CSV

function 안녕하세요(你好)

return 你好

end

println("this is a demo for read csv file.")

function 读取电子表格(表格路径)

CSV.read(表格路径,delim = ' ',datarow=1)

end

问候语 = 안녕하세요("你...

Julia教程8 读写文件操作

zhanghaijun2013的博客

03-31

4914

文章目录IO操作读文件操作写文件操作csv文件JLD2IOBuffer

在网易云课堂上直接搜索:Julia教程 ,就可以找到,教程的全名是:Julia教程 从入门到进阶

这是国内第一个免费的完整的Julia视频教程,非常适合Julia的入门。有兴趣的朋友可以去学习一下。

教程链接:

Julia教程

欢迎关注微信公众号:Quant_Times

IO操作

在对文件的操作中,如果我们未指定文件的路径,...

JLD.jl:保存和加载julia变量,同时保留本机类型

02-03

JLD.jl:保存和加载julia变量,同时保留本机类型

CSVReaders.jl:Julia 的 CSV 解析工具包

07-07

CSVReaders 是一个 beta 包,旨在提供标准化的 API,用于将 CSV 文件读入任意 Julia 数据结构。 为了添加对新数据结构的支持,用户必须实现一组标准的功能。 有关示例实现,请参阅src/interface目录。 该软件包有几...

KungIChi.jl:Julia中的数据验证和设置管理

04-07

Julia的港数据验证和设置管理使用python类型注释只需在Julia中使用。 因此,此软件包仅为您提供一些方便的验证工具和一些用于定义验证的语法糖。 它重新导出Configurations的接口,因此您不必显式依赖...

julia_builder:使用灵活的DSL轻松将查询导出到csv

05-10

Julia可以帮助您创建灵活的构建器,以轻松地将您的查询导出到csv(目前)。 安装 将此行添加到您的应用程序的Gemfile中: gem 'julia_builder' 然后执行: $ bundle 或将其自己安装为: $ gem install julia_...

Statistics With Julia_数据开发_统计学习_Julia统计学机器学习_

10-01

基于julia语言的数据分析好书,涉及统计学,机器学习,数据分析及其可视化等相关内容

Plots.jl:Julia可视化和数据分析的强大便利

02-03

Plots.jl:Julia可视化和数据分析的强大便利

HDF5.jl:从Julia中以HDF5文件格式保存和加载数据

02-03

HDF5.jl:从Julia中以HDF5文件格式保存和加载数据

FunctionalCollections.jl:Julia的功能性和持久性数据结构

05-28

功能集合 Julia的功能性和持久性数据结构。 这是一项正在进行的工作,目前尚未针对性能进行优化。 注意: FunctionalCollections的master分支适用于Julia v0.7及更高版本。 对于Julia的早期版本,请使用...

Julia数据处理常用包_DataFrames包测试3_2020-02-20Th

01-07

#@ 前面我们学习和测试了DataFrames包的特征和函数功能,下面我们接着本次的测试:利用DataFrames包读取.csv文件中的数据。 ## 1.创建一个.csv文件 using DataFrames using DelimitedFiles using CSV mycsv1 = open...

Julia如何结合金融数据和交易平台呢?

03-26

作为一个 AI 语言模型,我可以提供一些建议,但不保证完全正确。

Julia 是一门高性能语言,它可以用于数据分析和金融建模。如果要结合金融数据和交易平台,可以考虑以下几个方面:

1. 数据源的选择:要获取金融数据,需要选择合适的数据源。常见的数据源有 Bloomberg、Reuters、Quandl 等,它们都提供了 API 接口,可以用 Julia 编写代码获取数据。

2. 数据处理和建模:得到数据之后,可以用 Julia 进行数据清洗、处理和建模。Julia 有许多扩展包可供选择,包括金融模型、时间序列分析、机器学习模型等。这些包可以用来构建模型和预测未来市场走势。

3. 交易平台的集成:将模型应用到实际交易中,需要将模型和交易平台集成起来。有些交易平台提供了 API 接口,可以用代码连接和执行交易。如果没有现成的 API,可以考虑使用 Selenium 等工具模拟人工交易。

需要注意的是,金融交易涉及风险,建议在实际操作中进行充分的风险评估和测试。

“相关推荐”对你有帮助么?

非常没帮助

没帮助

一般

有帮助

非常有帮助

提交

YonminMa

CSDN认证博客专家

CSDN认证企业博客

码龄5年

暂无认证

78

原创

3万+

周排名

6万+

总排名

33万+

访问

等级

3682

积分

5920

粉丝

503

获赞

220

评论

2326

收藏

私信

关注

热门文章

百度网盘下载提速方法(更新于2020年6月)

52240

智能寻迹(循迹)小车项目思路 + 代码

44782

Python爬虫小白教程(五)—— 多线程爬虫

12977

java实现中国象棋1:界面的制作

11635

Julia导入和导出数据(CSV、TXT、JLD)

9148

分类专栏

RocketMQ

3篇

Spring Boot

3篇

项目

6篇

spring

4篇

LeetCode

8篇

深度学习

6篇

面试题

7篇

计算机网络

笔记

1篇

LaTeX

2篇

设计模式

1篇

爬虫

5篇

JVM

2篇

Julia

4篇

搜索算法

1篇

Linux

1篇

Matlab

1篇

java

21篇

最新评论

java实现中国象棋4:悔棋及重新开始

2301_81308505:

大佬,麻烦问一下,怎么在做好的象棋Java程序里面添加信息提示和悔棋功能

UEFI模式下win10安装Ubuntu双系统出现的问题总结

微醺早餐奶:

可是文件夹里grubx64和mmx64都有,怎么改名呢

Python爬虫小白教程(五)—— 多线程爬虫

Da_Shan_:

range(a, b) 是前闭后开的

Matlab模拟退火算法解决TSP问题

m0_62805048:

飞行点坐标源文件有吗 很急老哥

智能寻迹(循迹)小车项目思路 + 代码

沐梳清风:

有没有什么创新的点

您愿意向朋友推荐“博客详情页”吗?

强烈不推荐

不推荐

一般般

推荐

强烈推荐

提交

最新文章

RocketMQ 5.1.0 源码详解 | Producer 发送流程

RocketMQ 5.1.0 源码详解 | Producer 启动流程

RocketMQ5.1 NameServer 路由管理

2023年11篇

2022年1篇

2021年10篇

2020年39篇

2019年18篇

目录

目录

分类专栏

RocketMQ

3篇

Spring Boot

3篇

项目

6篇

spring

4篇

LeetCode

8篇

深度学习

6篇

面试题

7篇

计算机网络

笔记

1篇

LaTeX

2篇

设计模式

1篇

爬虫

5篇

JVM

2篇

Julia

4篇

搜索算法

1篇

Linux

1篇

Matlab

1篇

java

21篇

目录

评论

被折叠的  条评论

为什么被折叠?

到【灌水乐园】发言

查看更多评论

添加红包

祝福语

请填写红包祝福语或标题

红包数量

红包个数最小为10个

红包总金额

红包金额最低5元

余额支付

当前余额3.43元

前往充值 >

需支付:10.00元

取消

确定

下一步

知道了

成就一亿技术人!

领取后你会自动成为博主和红包主的粉丝

规则

hope_wisdom 发出的红包

实付元

使用余额支付

点击重新获取

扫码支付

钱包余额

0

抵扣说明:

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

余额充值

JLD一4G20发动机怎么样? - 知乎

JLD一4G20发动机怎么样? - 知乎首页知乎知学堂发现等你来答​切换模式登录/注册发动机内燃机汽车发动机通用电气航空汽油机JLD一4G20发动机怎么样?关注者1被浏览3,726关注问题​写回答​邀请回答​好问题​添加评论​分享​1 个回答默认排序熊猫月亮会让你想起我​ 关注 这台发动机是吉利以三菱发动机为原型,逆向研发的一款发动机。在说这台发动机之前,我想先说一下三菱的历史,如今国产车在人们心中的形象已经慢慢有了起色,很多人买车不再只看合资车,这与国产车这些年的发展离不开,然而国产车的发展也离不开三菱,毕竟三菱也算是国产汽车的奶妈。 三菱在量产车上无论设计还是营销都很失败,但在基础工业上收获甚丰,不少国内外车企采购它的发动机配件,如发动机总成、涡轮增压器。和东南合作的V3菱悦卖了这么多年,机械搭配和操控精神上在当年让国产品牌10万以下A级车难以超越,三菱的机械不错,但卖货的本事却很低。在日系车中,三菱的地位并不高,但是三菱汽车的发动机特别出名,有自己的特点,三菱汽车发动机采用军工技术,在日系发动机中是佼佼者的存在,有很多的跑车都想借用三菱发动机,三菱发动机的安全性更高、稳定性更强。三菱汽车对汽车界最大的贡献就是燃油发动机,并且我们大部分国产车都使用过三菱发动机,改革开放初期,汽车工业发展曲折,关键技术自主研发能力十分薄弱,外国轿车洪水般涌入中国。为了迅速提高中国轿车生产能力和技术水平,我国汽车工业开始走上与国外汽车企业合作、引进消化外国先进技术的发展道路。与此同时,随着国家“军民一体化”政策的不断深入,国防科技工业逐步实行军民结合,以四个现代化建设带动国防现代化。航天、航空、兵器等军工总公司改组为10个集团公司,实行合同制,实现政企分开、供需分开,从而使中国国防工业走上了“军民兼容”、“军民结合”的道路。航天三菱正是在这样的背景下,开始了一段助力中国汽车工业振兴的旅程。 三菱在1997年和1998年分别在中国投资建立了沈阳航天三菱汽车发动机制造有限公司(简称沈航三菱)和哈尔滨东安汽车发动机制造有限公司(简称东安三菱),直列四缸发动机一直都是汽车发动机中应用最多的一种,而那时候的三菱,旗下的四缸发动机在业界取得过很高的评价,4G1系列、4G3系列、4G5系列、4G6系列发动机最为有名,三菱把4G1系列分给东安三菱生产(东安是最早生产4G系列日本发动机的企业,因为一些原因还返销过日本,但因为控股、技术等原因,大多数东安三菱不能直接使用三菱标志,不同的是,后来的沈阳航天三菱可以使用三菱Logo。),把4G6系列分给沈航三菱生产。 历史翻到25年前(2022年8月12日是沈航三菱的生日),中国乘用车发动机发展史苍白一片,技术匮乏与制造工艺的不精良,让发动机问题频出,当年的三菱愿意出售老旧的技术给中国人用,也正是出售的这些发动机,让一部分中国企业走上的快车道。尽管沈航三菱和东安三菱生产的三菱发动机不是很优秀的发动机,但是至少要比很多自主品牌刚开始时研发、生产的发动机要强出很多。这些挂着三菱标,代号4G系列的发动机,至少在稳定性、可靠性方面还是比较不错的,在没有更好的解决方案的前提之下,采购来自这两家的三菱发动机,似乎也是更好的选择,长安、长城、奇瑞、吉利等车企,之前都是这两家发动机供应商的重要客户。至少,有发动机可用了,即便这些是淘汰的发动机,但架不住技术稳定,即便油耗高、噪音大,但也无妨,这也就开启了开启了国产三菱发动机时代。 红头4G63 曾经有一段时间,大家买国产车也就是冲着三菱的发动机去,毕竟三菱LOGO能让人安心,那串MITSUBISHI英文意味着可靠耐用。另外,三菱发动机的MIVEC可变气门正时系统,一度也让国产车型垂涎,因为它代表着高效和性能。可以看到,三菱发动机很长一段时间内,在国内都代表着先进、可靠。不过,随着科技的不断发展,国产车也开始研发自己的发动机,比如长城、奇瑞、吉利等等,大部分国产车不再使用国外的发动机,因此三菱就此没落,如今,说三菱发动机比自主发动机可靠估计还有人支持,但你要是说三菱发动机比自主发动机先进,那估计很多人会不服气,甚至在有些人眼里,三菱发动机已经成为了“不思进取”甚至是“落后”的代表,三菱发动机如今的使用份额越来越少,就像我们奇瑞和吉利在自主研发方面,都有十佳发动机的出现。 “JLD-4G20是吉利GeTec效绿色节能发动机系列产品之一,独立自主研发的。”——来自官方的介绍,具体参数到处都有,就不凑字数了,简单介绍一下:采用全铝缸体以实现发动机的轻量化,直列4缸,排量为2.0L,自然吸气。 作为一名自吸爱好者,前些年投了吉利一票,入手了一台使用此发动机的博越。吉利最开始依靠的是丰田的8A系列发动机,不过,由于丰田起诉吉利商标以及动力侵权,这也直接导致吉利无法再使用这一技术,可以说,吉利目前主流的发动机都不是纯正向自主研发的。个人认为JLD-4G20发动机就是逆向研制三菱4G63的发动机的产物,从参数看这台发动机和4G63比起来简直惨不忍睹,实际驾驶只能说勉强够用,毕竟博越自重约1.5T,也就是说,20多年仍然没有吃透核心技术。相比有些自主品牌车企,高喊着自己已经掌握了多少项核心技术,其实大多数都是在吹牛的情况下,吉利还算有自知之明。 本月提了一台领克09,同价位简直是无敌的存在,真香。发布于 2022-08-18 01:21​赞同 4​​添加评论​分享​收藏​喜欢收起​​

JLD · Julia Packages

JLD · Julia Packages

Search

Visit Github

File Issue

Email Request

Learn More

Sponsor Project

Visit Github

File Issue

Email Request

Learn More

Sponsor Project

JLD.jl

Saving and loading julia variables while preserving native types

Author

JuliaIO

Sub Category

Automata

Category

Programming Paradigms

Github

Popularity

259 Stars

Updated Last

11 Months Ago

Started In

July 2015

JLD.jl

Save and load variables in Julia Data format (JLD)

JLD, for which files conventionally have the extension .jld, is a

widely-used format for data storage with the Julia programming

language. JLD is a specific "dialect" of HDF5, a

cross-platform, multi-language data storage format most frequently

used for scientific data. By comparison with "plain" HDF5, JLD files

automatically add attributes and naming conventions to preserve type

information for each object.

For lossless storage of arbitrary Julia objects, the only other

complete solution appears to be Julia's serializer, which can be

accessed via the serialize and deserialize commands. However,

because the serializer is also used for inter-process communication,

long-term backwards compatibility is currently uncertain. (The

JLDArchives repository exists to test compatibility of older JLD file

formats.) If you choose to save data using the serializer, please use

the file extension .jls to distinguish the files from .jld files.

Note: You should only read JLD files from trusted sources, as JLD files are capable of executing arbitrary code when read in.

Installation

Within Julia, use the package manager:

Pkg.add("JLD")

Quickstart

To use the JLD module, begin your code with

using JLD

If you just want to save a few variables and don't care to use the more

advanced features, then a simple syntax is:

t = 15

z = [1,3]

save("/tmp/myfile.jld", "t", t, "arr", z)

# or equivalently:

@save "/tmp/myfile.jld" t z

Here we're explicitly saving t and z as "t" and "arr" within

myfile.jld. You can alternatively pass save a dictionary; the keys must be

strings and are saved as the variable names of their values within the JLD

file. You can read these variables back in with

d = load("/tmp/myfile.jld")

which reads the entire file into a returned dictionary d. Or you can be more

specific and just request particular variables of interest. For example, z = load("/tmp/myfile.jld", "arr") will return the value of arr from the file

and assign it back to z.

JLD uses the FileIO package to provide a generic

interface to save and load files. However this means that the user needs to

explicitly request for the JLD format to be used while saving a new file.

save("/tmp/foo","bar",0.0) # ambiguous

save("/tmp/foo.jld","bar",0.0) # JLD format is inferred from the file extension

using FileIO; save(File(format"JLD","/tmp/foo"),"bar",0.0) # JLD format explicitly requested using FileIO

This problem is not encountered while loading a JLD file because FileIO can use

magic bytes at the beginning of the file to infer its data format.

There are also convenience macros @save and @load that work on the

variables themselves.

@save "/tmp/myfile.jld" t z

# or

@save compress=true "/tmp/myfile.jld" t z

will create a file with just t and z, with or without compression.

If you don't mention any variables, then @save saves all the variables in the

current module. Conversely, @load will pop the saved variables directly into

the global workspace of the current module.

However, keep in mind that these macros have significant limitations: for example,

you can't use @load inside a function, there are constraints on using string

interpolation inside filenames, etc. These limitations stem

from the fact that Julia compiles functions to machine code before evaluation,

so you cannot introduce new variables at runtime or evaluate expressions

in other workspaces.

The save and load functions do not have these limitations, and are therefore

recommended as being considerably more robust, at the cost of some slight

reduction in convenience.

More fine-grained control can be obtained using functional syntax:

jldopen("mydata.jld", "w") do file

write(file, "A", A) # alternatively, say "@write file A"

end

c = jldopen("mydata.jld", "r") do file

read(file, "A")

end

This allows you to add variables as they are generated to an open JLD file.

You don't have to use the do syntax (file = jldopen("mydata.jld", "w") works

just fine), but an advantage is that it will automatically close the file (close(file))

for you, even in cases of error.

Julia's high-level wrapper, providing a dictionary-like interface, may

also be of interest:

using JLD, HDF5

jldopen("test.jld", "w") do file

g = create_group(file, "mygroup") # create a group

g["dset1"] = 3.2 # create a scalar dataset inside the group

g["dest2"] = rand(2,2)

end

Note that the features of HDF5 generally can also be used on JLD files.

Types and their definitions

You can save objects that have user-defined type; in a fresh Julia session, before loading those objects these types need to be defined. If no definition is available, the JLD module will automatically create the types for you. However, it's important to note that MyType, defined automatically by JLD, is not the same MyType as defined in an external module---in particular, module functions will not work for types defined by JLD. To ensure that loaded types have the full suite of behaviors provided by their definition in external modules, you should ensure that such modules are available before reading such variables from a .jld file.

To ensure automatic loading of modules, use addrequire to specify any dependencies. For example, suppose you have a file "MyTypes.jl" somewhere on your default LOAD_PATH, defined this way:

module MyTypes

export MyType

struct MyType

value::Int

end

end

and you have an object x of type MyType. Then save x in the following way:

jldopen("somedata.jld", "w") do file

addrequire(file, MyTypes)

write(file, "x", x)

end

This will cause "MyTypes.jl" to be loaded automatically whenever "somedata.jld" is opened.

If you have performance problems...

Please see the complete documentation, particularly the section about custom serializers.

Complete documentation

More extensive documentation, including information about the JLD

format conventions, can be found in the doc directory.

The test directory contains a number of test scripts that also

demonstrate usage.

Credits

Simon Kornblith and Tim Holy (co-maintainers and primary authors)

Tom Short contributed to string->type conversion

Thanks also to the users who have reported bugs and tested fixes

Required Packages

Compat

FileIO

HDF5

Requires

Used By Packages

Adjacently

AutoLandmarking

AutomationLabs

AvailablePotentialEnergyFramework

BeastUtils

BOMBs

BosonSampling

BrkgaMpIpr

CanDecomp

ClimateTasks

FCA

FEHM

FSInteraction

GeoThermalCloud

GridUtilities

Hydro

Intan

LightPropagation

LoadTensorDecompositions

Mads

MetaICVI

MonteCarloObservable

MosimoBase

MultiscaleGraphSignalTransforms

NEOs

NetworkHistogram

NMFk

NTFk

NTNk

ODINN

ParticleScattering

Perceptrons

PhylogeneticFactorAnalysis

PhysicsInformedML

PLSRegressor

PolaronMobility

PyCallJLD

Qaintellect

Qaintessent

Qaintmodels

RDMREopt

RelativisticDynamics

REopt

Schistoxpkg

Skyler

SmartML

SocialSolver

StorageGraphs

ViscousFlow

View all packages

Julia Packages

This website serves as a package browsing tool for the Julia programming language. It works by aggregating various sources on Github to help you find your next package.

By analogy, Julia Packages operates much like PyPI, Ember Observer, and Ruby Toolbox do for their respective stacks.

View Github

GitHub - JuliaIO/JLD.jl: Saving and loading julia variables while preserving native types

GitHub - JuliaIO/JLD.jl: Saving and loading julia variables while preserving native types

Skip to content

Toggle navigation

Sign in

Product

Actions

Automate any workflow

Packages

Host and manage packages

Security

Find and fix vulnerabilities

Codespaces

Instant dev environments

Copilot

Write better code with AI

Code review

Manage code changes

Issues

Plan and track work

Discussions

Collaborate outside of code

Explore

All features

Documentation

GitHub Skills

Blog

Solutions

For

Enterprise

Teams

Startups

Education

By Solution

CI/CD & Automation

DevOps

DevSecOps

Resources

Learning Pathways

White papers, Ebooks, Webinars

Customer Stories

Partners

Open Source

GitHub Sponsors

Fund open source developers

The ReadME Project

GitHub community articles

Repositories

Topics

Trending

Collections

Pricing

Search or jump to...

Search code, repositories, users, issues, pull requests...

Search

Clear

Search syntax tips

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

Sign up

You signed in with another tab or window. Reload to refresh your session.

You signed out in another tab or window. Reload to refresh your session.

You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

JuliaIO

/

JLD.jl

Public

Notifications

Fork

54

Star

273

Saving and loading julia variables while preserving native types

License

MIT license

273

stars

54

forks

Branches

Tags

Activity

Star

Notifications

Code

Issues

97

Pull requests

1

Actions

Projects

0

Wiki

Security

Insights

Additional navigation options

Code

Issues

Pull requests

Actions

Projects

Wiki

Security

Insights

JuliaIO/JLD.jl

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

 masterBranchesTagsGo to fileCodeFolders and filesNameNameLast commit messageLast commit dateLatest commit History818 Commits.github.github  docdoc  srcsrc  testtest  .gitignore.gitignore  LICENSE.txtLICENSE.txt  Project.tomlProject.toml  README.mdREADME.md  View all filesRepository files navigationREADMEMIT licenseJLD.jl

Save and load variables in Julia Data format (JLD)

JLD, for which files conventionally have the extension .jld, is a

widely-used format for data storage with the Julia programming

language. JLD is a specific "dialect" of HDF5, a

cross-platform, multi-language data storage format most frequently

used for scientific data. By comparison with "plain" HDF5, JLD files

automatically add attributes and naming conventions to preserve type

information for each object.

For lossless storage of arbitrary Julia objects, the only other

complete solution appears to be Julia's serializer, which can be

accessed via the serialize and deserialize commands. However,

because the serializer is also used for inter-process communication,

long-term backwards compatibility is currently uncertain. (The

JLDArchives repository exists to test compatibility of older JLD file

formats.) If you choose to save data using the serializer, please use

the file extension .jls to distinguish the files from .jld files.

Note: You should only read JLD files from trusted sources, as JLD files are capable of executing arbitrary code when read in.

Installation

Within Julia, use the package manager:

Pkg.add("JLD")

Quickstart

To use the JLD module, begin your code with

using JLD

If you just want to save a few variables and don't care to use the more

advanced features, then a simple syntax is:

t = 15

z = [1,3]

save("/tmp/myfile.jld", "t", t, "arr", z)

# or equivalently:

@save "/tmp/myfile.jld" t z

Here we're explicitly saving t and z as "t" and "arr" within

myfile.jld. You can alternatively pass save a dictionary; the keys must be

strings and are saved as the variable names of their values within the JLD

file. You can read these variables back in with

d = load("/tmp/myfile.jld")

which reads the entire file into a returned dictionary d. Or you can be more

specific and just request particular variables of interest. For example, z = load("/tmp/myfile.jld", "arr") will return the value of arr from the file

and assign it back to z.

JLD uses the FileIO package to provide a generic

interface to save and load files. However this means that the user needs to

explicitly request for the JLD format to be used while saving a new file.

save("/tmp/foo","bar",0.0) # ambiguous

save("/tmp/foo.jld","bar",0.0) # JLD format is inferred from the file extension

using FileIO; save(File(format"JLD","/tmp/foo"),"bar",0.0) # JLD format explicitly requested using FileIO

This problem is not encountered while loading a JLD file because FileIO can use

magic bytes at the beginning of the file to infer its data format.

There are also convenience macros @save and @load that work on the

variables themselves.

@save "/tmp/myfile.jld" t z

# or

@save compress=true "/tmp/myfile.jld" t z

will create a file with just t and z, with or without compression.

If you don't mention any variables, then @save saves all the variables in the

current module. Conversely, @load will pop the saved variables directly into

the global workspace of the current module.

However, keep in mind that these macros have significant limitations: for example,

you can't use @load inside a function, there are constraints on using string

interpolation inside filenames, etc. These limitations stem

from the fact that Julia compiles functions to machine code before evaluation,

so you cannot introduce new variables at runtime or evaluate expressions

in other workspaces.

The save and load functions do not have these limitations, and are therefore

recommended as being considerably more robust, at the cost of some slight

reduction in convenience.

More fine-grained control can be obtained using functional syntax:

jldopen("mydata.jld", "w") do file

write(file, "A", A) # alternatively, say "@write file A"

end

c = jldopen("mydata.jld", "r") do file

read(file, "A")

end

This allows you to add variables as they are generated to an open JLD file.

You don't have to use the do syntax (file = jldopen("mydata.jld", "w") works

just fine), but an advantage is that it will automatically close the file (close(file))

for you, even in cases of error.

Julia's high-level wrapper, providing a dictionary-like interface, may

also be of interest:

using JLD, HDF5

jldopen("test.jld", "w") do file

g = create_group(file, "mygroup") # create a group

g["dset1"] = 3.2 # create a scalar dataset inside the group

g["dest2"] = rand(2,2)

end

Note that the features of HDF5 generally can also be used on JLD files.

Types and their definitions

You can save objects that have user-defined type; in a fresh Julia session, before loading those objects these types need to be defined. If no definition is available, the JLD module will automatically create the types for you. However, it's important to note that MyType, defined automatically by JLD, is not the same MyType as defined in an external module---in particular, module functions will not work for types defined by JLD. To ensure that loaded types have the full suite of behaviors provided by their definition in external modules, you should ensure that such modules are available before reading such variables from a .jld file.

To ensure automatic loading of modules, use addrequire to specify any dependencies. For example, suppose you have a file "MyTypes.jl" somewhere on your default LOAD_PATH, defined this way:

module MyTypes

export MyType

struct MyType

value::Int

end

end

and you have an object x of type MyType. Then save x in the following way:

jldopen("somedata.jld", "w") do file

addrequire(file, MyTypes)

write(file, "x", x)

end

This will cause "MyTypes.jl" to be loaded automatically whenever "somedata.jld" is opened.

If you have performance problems...

Please see the complete documentation, particularly the section about custom serializers.

Complete documentation

More extensive documentation, including information about the JLD

format conventions, can be found in the doc directory.

The test directory contains a number of test scripts that also

demonstrate usage.

Credits

Simon Kornblith and Tim Holy (co-maintainers and primary authors)

Tom Short contributed to string->type conversion

Thanks also to the users who have reported bugs and tested fixes

About

Saving and loading julia variables while preserving native types

Topics

serializer

julia

data-storage

hdf5-format

jld-format

Resources

Readme

License

MIT license

Activity

Custom properties

Stars

273

stars

Watchers

24

watching

Forks

54

forks

Report repository

Releases

26

v0.13.4

Latest

Dec 30, 2023

+ 25 releases

Packages

0

No packages published

Contributors

64

+ 50 contributors

Languages

Julia

100.0%

Footer

© 2024 GitHub, Inc.

Footer navigation

Terms

Privacy

Security

Status

Docs

Contact

Manage cookies

Do not share my personal information

You can’t perform that action at this time.

JLD2 与 HDF5 - 知乎

JLD2 与 HDF5 - 知乎首发于地面气象短临天气预报系统设计切换模式写文章登录/注册JLD2 与 HDF5hxfcalf程序员:街树飘影未见尘 潭月潜水了无声 般若观照心空静...Julia JLD2 for NetcdfBasics · Julia Data Format请问,计算机语言Julia 的JLD2 的目的是什么?Julia语言的JLD2是一种高效的数据格式,用于存储和读取Julia程序中生成的数据。它的主要目的是提供快速、可靠和可扩展的数据存储解决方案,以便Julia用户可以轻松地在不同的计算机之间共享和重复使用数据。JLD2支持多种数据类型,包括数值、字符串、数组、图像和自定义数据类型,同时也提供了高级压缩功能来减小数据文件的大小。JLD2产生文件是否是HDF格式的?是的,JLD2产生的文件的格式是HDF5格式。JLD2是Julia语言的一个库,用于将Julia程序中生成的多维数组和自定义数据类型存储到磁盘上。JLD2在HDF5的基础上进行了改进和扩展,提供了更高效、更可靠、更易用的数据存储方案。因此,JLD2产生的文件不仅可以被Julia程序读取和解析,也可以被其他支持HDF5格式的程序读取和处理。JLD2对其它语言产生的HDF5格式文件,能够获取吗?是的,JLD2可以获取其他语言产生的HDF5格式文件。因为JLD2是基于HDF5格式开发的,所以它可以与其他支持HDF5格式的语言或程序进行交互,例如Python、MATLAB和C++等。你可以使用JLD2读取任何符合HDF5标准的文件,而不仅仅局限于Julia生成的文件。 具体来说,你可以使用JLD2提供的`JLD2.load()`函数加载其他语言产生的HDF5格式文件,并将其转换为Julia中的数据类型,比如数组和结构体。但需要注意的是,在加载之前需要确保你已经安装了HDF5库,并且需要用到相应的hdf5和hdf5_hl的库。此外,还应该确保你已经在Julia中加载了JLD2库。 下面是一个加载HDF5文件并提取数据的示例代码:using JLD2

# Load HDF5 file and extract data

jld_file = h5open("filename.h5", "r")

data = JLD2.load(jld_file, "dataset_name") # Replace dataset_name with the actual name of your dataset

close(jld_file)其中,`“filename.h5”`是你要加载的文件的名称或路径;`“dataset_name”`是你要提取的数据集的名称。在这个示例中,我们使用`h5open()`函数打开HDF5文件,然后使用`JLD2.load()`函数获取指定的数据集。最后我们关闭文件句柄,释放资源。NetCDF4数据模型 是 HDF5的子集对吗?是的,NetCDF4数据模型是HDF5的子集。NetCDF4使用了HDF5库的数据结构和I/O接口,并且建立在HDF5的数据格式之上,因此在一定程度上可以看做是HDF5的扩展。它提供了一种方便的方式来组织和存储科学数据,尤其是涉及到多维数组和元数据的数据类型。NetCDF4支持可扩展的数据描述、灵活的压缩机制以及快速的I/O操作,这些特性使它成为了科学计算领域广泛使用的一种数据格式。 在HDF5中,数据被组织成一个或多个数据集,每个数据集包含了一系列的数据项。NetCDF4按照相同的方式组织数据,同时还添加了一些其他的元数据信息,例如变量名称、单位、坐标轴、时间戳等。NetCDF4还支持各种不同的数据类型,包括整数、浮点数、字符串、枚举等等,这些数据类型与HDF5库中提供的数据类型类似但不完全相同。 总的来说,NetCDF4可以被看作是HDF5的一种应用程序,它在HDF5的基础上添加了更多的元数据和特殊数据类型,以满足科学计算等领域的需求。由于NetCDF4是建立在HDF5之上的,因此它能够兼容HDF5数据格式,并且可以使用同一套I/O接口进行读写操作。逻辑上JLD2也可以读取NetCDF4的文件吧?LD2和NetCDF4是两个不同的数据格式,它们之间并没有直接的逻辑关系。JLD2是Julia语言的一个库,用于将Julia程序中生成的多维数组和自定义数据类型存储到磁盘上,而NetCDF4则是一种用于存储科学数据的文件格式,具有多维数据、元数据、压缩等特性。 虽然JLD2和NetCDF4不是直接相关的数据格式,但是在Julia语言中可以使用其他的库来读取和处理NetCDF4格式的数据,例如NetCDF.jl和NCDatasets.jl。这些库可以将NetCDF4格式的数据读入到Julia中的数组和数据结构中,然后再借助JLD2将这些数据存储到磁盘上。另外,也可以使用其他的工具和库,例如Python中的xarray和netcdf4-python库,来读写NetCDF4格式的数据,并与Julia程序进行数据交换。FileIO 与JLD2是一种什么关系?FileIO和JLD2都是Julia语言中用于文件读写的库,但它们有着不同的设计思路和应用场景。

FileIO是一个通用的文件读写接口,它提供了一组统一的API,使得用户可以用相同的方式读写不同的文件格式,

如CSV、JSON、MAT、NPZ等。FileIO的优点是扩展性很强,只需要编写相应的IO包就可以在FileIO中进行读写操作,

无需改变调用方式。这种设计思路使得FileIO非常灵活,方便用户在不同的应用场景中进行文件读写操作,

而无需频繁地转换数据类型和格式。

JLD2则是专门用于Julia数据的序列化和反序列化的库,它可以将Julia数据类型(如数组、结构体、函数等)

存储到磁盘上,并能够快速地加载和恢复这些数据。JLD2的优点是非常高效,序列化和反序列化速度都非常快,

适用于处理大规模的数据集。

在使用FileIO时,可以借助JLD2进行Julia数据类型的读写操作;

或者使用其他的IO包,如CSV.jl、JSON.jl等进行其他格式的读写操作。

因此,FileIO和JLD2可以说是互补的关系,二者结合使用可以极大地简化Julia程序中的文件读写操作。当然可以,请见下面的代码:using FileIO, JLD2

# 将一个数组保存为JLD2文件

data = rand(10, 10)

save("data.jld2", "mydata", data)

# 从JLD2文件中读取数据

loaded_data = load("data.jld2", "mydata")

# 将数组保存为CSV文件

save("data.csv", loaded_data)

# 从CSV文件中读取数据

csv_data = load("data.csv")

# 对比读取的CSV数据是否与源数据相等

println(all(loaded_data .== csv_data)) # true在这段代码中,我们先使用JLD2将一个随机生成的10x10的数组保存为data.jld2文件。接着,我们使用load函数从文件中读取数据,并将其保存为变量loaded_data。然后,我们使用save函数将loaded_data保存为data.csv文件。最后,我们使用load函数从CSV文件中读取数据,并且将读取出来的数据与源数据进行比较,结果表明读取的CSV数据与源数据相等。 可以看到,在这个例子中,FileIO和JLD2共同协作,完成了对数据的读写操作,使得我们不需要过多的关注具体的文件格式和类型,而能够轻松地进行文件读写。您工作非常棒哟。我想以NetCDF4 为背景,请再为我写一段FileIO和JLD2协同互补的演示代码,好吗?当然可以。以下是一个简单的示例代码,展示如何使用FileIO与JLD2一起使用NetCDF4格式。using FileIO, NetCDF, JLD2

# 创建一个NetCDF数据文件

ncfile = NetCDF.create("data.nc")

# 定义NetCDF变量

var1 = NetCDF.defVar(ncfile, "var1", Float64, ("dim1","dim2"))

# 设置变量属性

NetCDF.putAtt(ncfile, var1, "long_name", "Variable 1")

NetCDF.putAtt(ncfile, var1, "units", "meters")

# 定义NetCDF维度

dim1 = NetCDF.defDim(ncfile, "dim1", 10)

dim2 = NetCDF.defDim(ncfile, "dim2", 5)

# 写入数据到NetCDF文件

data = rand(10,5)

NetCDF.putVar(ncfile, var1, data)

# 关闭NetCDF文件

NetCDF.close(ncfile)

# 使用FileIO和JLD2读取NetCDF文件

data = load("data.nc")["var1"]我想以NCDataset为背景,请再为我写一段FileIO和JLD2协同互补的演示代码,好吗?当然可以。以下是一个简单的示例代码,展示如何使用FileIO与JLD2一起使用NCDataset。using FileIO, NetCDF, JLD2

# 创建一个NetCDF数据文件

ncfile = NetCDF.create("data.nc")

# 定义NetCDF变量

var1 = NetCDF.defVar(ncfile, "var1", Float64, ("dim1","dim2"))

# 设置变量属性

NetCDF.putAtt(ncfile, var1, "long_name", "Variable 1")

NetCDF.putAtt(ncfile, var1, "units", "meters")

# 定义NetCDF维度

dim1 = NetCDF.defDim(ncfile, "dim1", 10)

dim2 = NetCDF.defDim(ncfile, "dim2", 5)

# 写入数据到NetCDF文件

data = rand(10,5)

NetCDF.putVar(ncfile, var1, data)

# 关闭NetCDF文件

NetCDF.close(ncfile)

# 使用FileIO和JLD2读取NetCDF文件

ds = NCDataset("data.nc")

data = ds["var1"][:,:]

# 将数据保存为JLD2格式

save("data.jld2", "var1", data)

# 从JLD2文件中读取数据

data = load("data.jld2")["var1"]

这段代码与之前的示例非常相似,唯一的区别是在读取数据时使用了NCDataset。

我们还将NetCDF数据保存为JLD2格式,并使用FileIO和JLD2来读取它。请为我推荐一个能够可视化显示HDF5格式文件的桌面软件吗?Sure,下面为您推荐几个能够可视化显示HDF5格式文件的桌面软件:1. HDFView:官方提供的免费软件,适用于Windows、MacOS和Linux系统。它可以浏览和编辑HDF5和HDF4文件,支持多种数据类型和元数据的查看和修改。2. HDF Compass:也是一款开源的HDF5数据浏览和编辑工具,支持多种操作系统,包括Windows、MacOS和Linux。它的界面非常简洁直观,可以方便地查看和编辑HDF5文件中的数据和元数据。3. Panoply:一款针对科学数据可视化的免费软件,支持多种数据格式,包括HDF、NetCDF、GRIB等。它可以将HDF5文件中的多维数据展示为图像或动画,支持交互式的缩放和漫游操作。4. ParaView:一个功能强大的科学数据可视化软件,支持多种数据格式,包括HDF、NetCDF、VTK等。它可以将HDF5文件中的数据可视化为三维图像和动画,并支持各种高级数据分析和处理操作。这些软件都提供了友好的界面和丰富的功能,可以满足不同用户的需求,您可以根据自己的实际需求选择使用。编辑于 2023-05-19 09:10・IP 属地北京Julia(编程语言)​赞同 1​​添加评论​分享​喜欢​收藏​申请转载​文章被以下专栏收录地面气象短临天气预报系统设计气候、大气动力、卫星气象学、深度学习下的短

julia中[JLD2]包的使用-hdf5格式的数据存取 - 简书

a中[JLD2]包的使用-hdf5格式的数据存取 - 简书登录注册写文章首页下载APP会员IT技术julia中[JLD2]包的使用-hdf5格式的数据存取昵称违法关注赞赏支持julia中[JLD2]包的使用-hdf5格式的数据存取一、包的介绍

JLD2 saves and loads Julia data structures in a format comprising a subset of HDF5, without any dependency on the HDF5 C library. It typically outperforms the JLD package (sometimes by multiple orders of magnitude) and often outperforms Julia's built-in serializer. While other HDF5 implementations supporting HDF5 File Format Specification Version 3.0 (i.e. libhdf5 1.10 or later) should be able to read the files that JLD2 produces, JLD2 is likely to be incapable of reading files created or modified by other HDF5 implementations. JLD2 does not aim to be backwards or forwards compatible with the JLD package.

JLD2来源于HDF5,但是它没有用hdf5的C库,完全自己动手纯julia实现,JLD2用来保存和加载Julia数据类型。以前有一个包叫JLD,(我没查过资料,不做评论),官网说JLD2性能远超JLD,甚至通常胜过Julia的内置序列化器

它的好处就是把julia的数据类型直接存在硬盘上,比如我们用的datafarame,直接系列化到硬盘上,而不是存成xecel或者csv然后在读取,以csv为例,虽然它的文件比较小,但是有可能会丢失一些数据类型信息。

另外,我们可以用键、值来保存信息,好处是方便数据提取。打个比方,回测时用到【指数】、【基金】、【股票】的k线,我们可以用组来标记【指数】、【基金】、【股票】,然后通过【股票代码】(key)直接提取某一只股票的k线(value)。

同理,计算好的回测指标,比如回测的时候,要快速提取【某只股票】在【某天】的【macd金叉】和【kd指标】数据,我们便可以提前把计算好的数据存成jld2的格式,用的时候,直接调入内存,进行快速提取,大大节约回测时间。特别是涉及到参数组合优化的时候,往往需要进行大量的计算(可能需要连续开机计算计算好几天),这时候,就能够展现key-value数据格式的优点。

给定一组参数,要选取最优组合的时候,往往涉及大量的计算,并且需要保存大量的中间计算结果。假如你要保存1600万个中间计算结果(组合状态 对应 表格数据),如果你把每个文件的直接写到硬盘上,估计很慢,而且再次复用的时候,读取速度又是一大麻烦。此时用字典来组织内存数据,当达到一定的大小后,挂上关键字信息分批写入jld2文件中

二、地址:

https://github.com/JuliaIO/JLD2.jl

三、安装

Pkg.add("JLD2")

四、save和load的几种使用方法

几种保存和读取jld2文件的方式

4.1 @save 和 @load

using JLD2

hello = "world"

foo = :bar

name = "张翠山"

@save "example.jld2" hello foo

@load "example.jld2"

@load "example.jld2" foo

@load "example.jld2" hello foo

@save "example.jld2" name

@load "example.jld2"

代码运行结果

image.png

以上我们使用save和load宏进行一个变量的保存和加载,如果我们有一个dataframe,能不能保存到jld2文件里呢?

mydf = DataFrame(姓名 = ["a","b","c","d","e"],年龄 = [1,2,3,4,5])

@save "example.jld2" mydf

@load "example.jld2"

你会发现,你并没有保存dataframe,那要如何操作呢?如何对键、值数据(字典)进行save和load?

4.2 FileIO中的save 和load

The save function accepts an AbstractDict yielding the key/value pairs, where the key is a string representing the name of the dataset and the value represents its contents。

FileIO中的save可以用来保存字典,注意,key只能是字符串。另外,在使用FileIO中的save和load的时候,不需要using JLD2,因为FileIO包会自动识别它操作的是啥。

using FileIO

mydict = Dict("df" => DataFrame(姓名 = ["a","b","c","d","e"],年龄 = [1,2,3,4,5]))

save("example.jld2", mydict)

load("example.jld2")

代码运行结果

image.png

注意: save也可以用来保存一般的变量,但是,它会进行装箱操作,看下面的例子:

save("example.jld2", "hello", "world", "foo", :bar) #它会自动装箱成Dict,按照key=>value逐对匹配

load("example.jld2")

"""

输出结果为:

Dict{String,Any} with 2 entries:

"hello" => "world"

"foo" => :bar

"""

save("example.jld2", "name", "张翠山") #这里不是增加一个键值对,而是用新装箱的dict把源文件覆盖重写

load("example.jld2")

"""

输出结果为:

Dict{String,Any} with 1 entry:

"name" => "张翠山"

"""

你以为保存了2个变量,实际上却是保存了一个键值对,哈哈哈

问题:我已经在jld2文件里保存了一个字典了,我怎样把一个新的【键值对】插入到jld2的字典中

4.3 FileIO中save和load的另一种用法

4.3.1 像操作普通文件一样操作jld2数据,多种open方式

f = jldopen("example.jld2", "r") # open read-only (default)

f = jldopen("example.jld2", "r+") # open read/write, failing if no file exists

f = jldopen("example.jld2", "w") # open read/write, overwriting existing file

f = jldopen("example.jld2", "a+") # open read/write, preserving contents of existing file or creating a new file

注意,f对象在使用完毕后,一定要close(f)

#读取hello键对应的值

f = jldopen("example.jld2", "r")

f["hello"] |> println

close(f) #记得关闭

#重置hello键对应的值

f = jldopen("example.jld2", "w")

f["hello"] = "你好"

f["hello"] |> println

close(f) #记得关闭

open的变种用法

4.3.2 open的变种用法

jldopen和do

jldopen("example.jld2", "a+") do file

file["bigdata111"] = randn(5)

file["bigdata112"] = randn(5)

file["bigdata113"] = randn(5)

end

4.4 Group的使用

Group是什么东西,你可以理解成字典中的一个key,每个key下面,又是一个字典。你可以理解成一个文件夹,通过这个文件夹的路径,我们可以访问这个文件夹下的文件。

using FileIO

save("example.jld2", Dict("hello" => "world", "foo" => :bar))

jldopen("example.jld2", "a+") do file

mygroup = JLD2.Group(file, "mygroup")

mygroup["mystuff"] = 42

mygroup["mystuff1"] = 43

end

load("example.jld2")

代码运行结果

image.png

红色部分为新建mygroup组下的节点数据

【注意事项】 用【@save】和【save】保存后的jld2文件,存在一定差别,如下图红色部所示:

image.png

其它问题:

1、加载JLD2文件的时候,出现关于”DataFrames“的警告,什么原因,之前都没遇到的?

┌ Warning: type DataFrames.DataFrame does not exist in workspace; reconstructing

└ @ JLD2 C:\Users\dengz.juliapro\JuliaPro_v1.4.2-1\packages\JLD2\4AJn8\src\data.jl:1190

┌ Warning: type DataFrames.Index does not exist in workspace; reconstructing

└ @ JLD2 C:\Users\dengz.juliapro\JuliaPro_v1.4.2-1\packages\JLD2\4AJn8\src\data.jl:1190

┌ Warning: type DataFrames.DataFrame does not exist in workspace; reconstructing

└ @ JLD2 C:\Users\dengz.juliapro\JuliaPro_v1.4.2-1\packages\JLD2\4AJn8\src\data.jl:1190

┌ Warning: type DataFrames.Index does not exist in workspace; reconstructing

└ @ JLD2 C:\Users\dengz.juliapro\JuliaPro_v1.4.2-1\packages\JLD2\4AJn8\src\data.jl:1190

原因:jld2数据文件里面有DataFrames的数据,但是程序开始没有using DataFrames的包,导致无法识别数据类型。using后,不在出现报警。

文中的谬误,还望不吝指正。

最后编辑于 :2021.04.26 19:30:41©著作权归作者所有,转载或内容合作请联系作者人面猴序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...沈念sama阅读 145,261评论 1赞 308死咒序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...沈念sama阅读 62,177评论 1赞 259救了他两次的神仙让他今天三更去死文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...开封第一讲书人阅读 96,329评论 0赞 214道士缉凶录:失踪的卖姜人 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...开封第一讲书人阅读 41,490评论 0赞 184港岛之恋(遗憾婚礼)正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...茶点故事阅读 49,353评论 1赞 262恶毒庶女顶嫁案:这布局不是一般人想出来的文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...开封第一讲书人阅读 39,028评论 1赞 179城市分裂传说那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...沈念sama阅读 30,611评论 2赞 276双鸳鸯连环套:你想象不到人心有多黑文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...开封第一讲书人阅读 29,383评论 0赞 171万荣杀人案实录序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...沈念sama阅读 32,749评论 0赞 215护林员之死正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...茶点故事阅读 29,460评论 2赞 219白月光启示录正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...茶点故事阅读 30,814评论 1赞 232活死人序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...沈念sama阅读 27,255评论 2赞 215日本核电站爆炸内幕正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...茶点故事阅读 31,752评论 3赞 214男人毒药:我在死后第九天来索命文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...开封第一讲书人阅读 25,685评论 0赞 9一桩弑父案,背后竟有这般阴谋文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...开封第一讲书人阅读 26,114评论 0赞 170情欲美人皮我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...沈念sama阅读 33,747评论 2赞 234代替公主和亲正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...茶点故事阅读 33,901评论 2赞 238推荐阅读更多精彩内容Spark的数据戏法聚焦在 Twitter 上关于Apache Spark的数据,目标是准备将来用于机器学习和流式处理应用的数据。 ...abel_cao阅读 2,631评论 1赞 12Julia 小白 Day 15 :本地实现基于深度学习的图片内容识别前情提要: 概述 本地环境:Windows、Mac 在线环境:无痛环境、定制无痛环境 快速学习:DataFrame..._KevinZhang_阅读 2,902评论 0赞 2菜鸟学习人工智能第七期:模块(就是程序)与Python常用的140个标准库推荐,文末有免费学习资料Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和...KangSmit的算法那些事儿阅读 1,831评论 0赞 3TF2 Keras (5) : 保存&加载本文是对官方文档[https://www.tensorflow.org/guide/keras/save_and_...数科每日阅读 1,852评论 0赞 0《机器学习实战:基于Scikit-Learn、Keras和TensorFlow(第二版)》第1...Aurélien 在写第二版时,对下半部分深度学习各章节的修订非常非常大(前面机器学习的部分更改只有10%,只是新...SeanCheney阅读 13,865评论 6赞 55评论0赞赞1赞赞赏更

Basics · Julia Data Format

Basics · Julia Data FormatJulia Data FormatBasicsReading and writing dataFile interfaceGroupsGotchasCustom SerializationCompressionInternals & DesignHDF5 CompatibilityAdvanced UsageLegacyVersionBasicsBasicsEdit on GitHubJulia Data Format - JLD2JLD2 saves and loads Julia data structures in a format comprising a subset of HDF5, without any dependency on the HDF5 C library. JLD2 is able to read most HDF5 files created by other HDF5 implementations supporting HDF5 File Format Specification Version 3.0 (i.e. libhdf5 1.10 or later) and similarly those should be able to read the files that JLD2 produces. JLD2 provides read-only support for files created with the JLD package.Reading and writing dataA new interface: jldsavejldsave makes use of julia's keyword argument syntax to store files, thus leveraging the parser and not having to rely on macros. To use it, writex = 1

y = 2

z = 42

# The simplest case:

jldsave("example.jld2"; x, y, z)

# it is equivalent to

jldsave("example.jld2"; x=x, y=y, z=z)

# You can assign new names selectively

jldsave("example.jld2"; x, a=y, z)

# and if you want to confuse your future self and everyone else, do

jldsave("example.jld2"; z=x, x=y, y=z)Compression and non-default IO types may be set via positional arguments.save_object and load_object functionsIf only a single object needs to stored and loaded from a file, one can use save_object and load_object functions.JLD2.save_object — Functionsave_object(filename, x)Stores an object x in a new JLD2 file at filename. If a file exists at this path, it will be overwritten.Since the JLD2 format requires that all objects have a name, the object will be stored as single_stored_object. If you want to store more than one object, use @save macro, jldopen or the FileIO API.ExampleTo save the string hello to the JLD2 file example.jld2:hello = "world"

save_object("example.jld2", hello)sourceJLD2.load_object — Functionload_object(filename)Returns the only available object from the JLD2 file filename (The stored object name is inconsequential). If the file contains more than one or no objects, the function throws an ArgumentError.For loading more than one object, use @load macro, jldopen or the FileIO API.ExampleTo load the only object from the JLD2 file example.jld2:hello = "world"

save_object("example.jld2", hello)

hello_loaded = load_object("example.jld2")sourcesave and load functionsThe save and load functions, provided by FileIO, provide a mechanism to read and write data from a JLD2 file. To use these functions, you may either write using FileIO or using JLD2. FileIO will determine the correct package automatically.The save function accepts an AbstractDict yielding the key/value pairs, where the key is a string representing the name of the dataset and the value represents its contents:using FileIO

save("example.jld2", Dict("hello" => "world", "foo" => :bar))The save function can also accept the dataset names and contents as arguments:save("example.jld2", "hello", "world", "foo", :bar)When using the save function, the file extension must be .jld2, since the extension .jld currently belongs to the previous JLD package.If called with a filename argument only, the load function loads all datasets from the given file into a Dict:load("example.jld2") # -> Dict{String,Any}("hello" => "world", "foo" => :bar)If called with a single dataset name, load returns the contents of that dataset from the file:load("example.jld2", "hello") # -> "world"If called with multiple dataset names, load returns the contents of the given datasets as a tuple:load("example.jld2", "hello", "foo") # -> ("world", :bar)File interfaceIt is also possible to interact with JLD2 files using a file-like interface. The jldopen function accepts a file name and an argument specifying how the file should be opened:using JLD2

f = jldopen("example.jld2", "r") # open read-only (default)

f = jldopen("example.jld2", "r+") # open read/write, failing if no file exists

f = jldopen("example.jld2", "w") # open read/write, overwriting existing file

f = jldopen("example.jld2", "a+") # open read/write, preserving contents of existing file or creating a new fileData can be written to the file using write(f, "name", data) or f["name"] = data, or read from the file using read(f, "name") or f["name"]. When you are done with the file, remember to call close(f).Like open, jldopen also accepts a function as the first argument, permitting do-block syntax:jldopen("example.jld2", "w") do file

file["bigdata"] = randn(5)

endGroupsIt is possible to construct groups within a JLD2 file, which may or may not be useful for organizing your data. You can create groups explicitly:jldopen("example.jld2", "w") do file

mygroup = JLD2.Group(file, "mygroup")

mygroup["mystuff"] = 42

endor implicitly, by saving a variable with a name containing slashes as path delimiters:jldopen("example.jld2", "w") do file

file["mygroup/mystuff"] = 42

end

# or save("example.jld2", "mygroup/mystuff", 42)Both of these examples yield the same group structure, which you can see at the REPL:julia> file = jldopen("example.jld2", "r")

JLDFile /Users/simon/example.jld2 (read-only)

└─ mygroup

└─ mystuffSimilarly, you can access groups directly:jldopen("example.jld2", "r") do file

@assert file["mygroup"]["mystuff"] == 42

endor using slashes as path delimiters:@assert load("example.jld2", "mygroup/mystuff") == 42When loading files with nested groups these will be unrolled into paths by default but yield nested dictionaries but with the nested keyword argument.load("example.jld2") # -> Dict("mygroup/mystuff" => 42)

load("example.jld2"; nested=true) # -> Dict("mygroup" => Dict("mystuff" => 42))Unpack.jl APIWhen additionally loading the UnPack.jl package, its @unpack and @pack! macros can be used to quickly save and load data from the file-like interface. Example:using UnPack

file = jldopen("example.jld2", "w")

x, y = rand(2)

@pack! file = x, y # equivalent to file["x"] = x; file["y"] = y

@unpack x, y = file # equivalent to x = file["x"]; y = file["y"]The group file_group = Group(file, "mygroup") can be accessed with the same file-like interface as the "full" struct.GotchasObjects are cached during loadingJLD2 caches objects during loading. It may give you the same object twice. This can lead to surprising results if you edit loaded arrays. Note, the underlying file is not being edited!julia> jldsave("demo.jld2", a=zeros(2))

julia> f = jldopen("demo.jld2")

JLDFile /home/isensee/demo.jld2 (read-only)

└─ a

julia> a = f["a"] # bind loaded array to name `a`

2-element Vector{Float64}:

0.0

0.0

julia> a[1] = 42; # editing the underlying array

julia> f["a"]

2-element Vector{Float64}:

42.0

0.0

julia> a=nothing # remove all references to the loaded array

julia> GC.gc(true) # call GC to remove the cache

julia> f["a"] # a new copy is loaded from the file

2-element Vector{Float64}:

0.0

0.0Cross-compatibilityJLD2 tries to write files in a way that allows you to load them on different operating systems and in particular both on 32bit and 64bit systems. However, many julia structs may be inherently different on different architectures making this task impossible. In particular, moving data from a 64bit system to a 32bit system is only guaranteed to work for basic datatypes.SecurityBeware of opening JLD2 files from untrusted sources. A malicious file may execute code on your computer. See e.g. here. To check a file, you can use JLD2DebugTools.jl to view what kinds of objects are stored.Custom Serialization »Powered by Documenter.jl and the Julia Programming Language.SettingsThemedocumenter-lightdocumenter-darkThis document was generated with Documenter.jl on Saturday 17 February 2024. Using Julia version 1.10.1.

JLD2 · Julia Packages

JLD2 · Julia Packages

Search

Visit Github

File Issue

Email Request

Learn More

Sponsor Project

Visit Github

File Issue

Email Request

Learn More

Sponsor Project

JLD2.jl

HDF5-compatible file format in pure Julia

Author

JuliaIO

Sub Category

Data Formats

Category

File IO

Github

Popularity

436 Stars

Updated Last

10 Months Ago

Started In

July 2015

JLD2

Documentation

Tests

CodeCov

Downloads

JLD2 saves and loads Julia data structures in a format comprising a subset of HDF5, without any dependency on the HDF5 C library.

JLD2 is able to read most HDF5 files created by other HDF5 implementations supporting HDF5 File Format Specification Version 3.0 (i.e. libhdf5 1.10 or later) and similarly those should be able to read the files that JLD2 produces. JLD2 provides read-only support for files created with the JLD package.

Reading and writing data

save and load functions

The save and load functions, provided by FileIO, provide a mechanism to read and write data from a JLD2 file. To use these functions, you may either write using FileIO or using JLD2. FileIO will determine the correct package automatically.

The save function accepts an AbstractDict yielding the key/value pairs, where the key is a string representing the name of the dataset and the value represents its contents:

using FileIO

save("example.jld2", Dict("hello" => "world", "foo" => :bar))

The save function can also accept the dataset names and contents as arguments:

save("example.jld2", "hello", "world", "foo", :bar)

When using the save function, the file extension must be .jld2, since the extension .jld currently belongs to the previous JLD package.

If called with a filename argument only, the load function loads all datasets from the given file into a Dict:

load("example.jld2") # -> Dict{String,Any}("hello" => "world", "foo" => :bar)

If called with a single dataset name, load returns the contents of that dataset from the file:

load("example.jld2", "hello") # -> "world"

If called with multiple dataset names, load returns the contents of the given datasets as a tuple:

load("example.jld2", "hello", "foo") # -> ("world", :bar)

A new interface: jldsave

jldsave makes use of julia's keyword argument syntax to store files,

thus leveraging the parser and not having to rely on macros. The new interface can be imported with using JLD2. To use it, write

using JLD2

x = 1

y = 2

z = 42

# The simplest case:

jldsave("example.jld2"; x, y, z)

# it is equivalent to

jldsave("example.jld2"; x=x, y=y, z=z)

# You can assign new names selectively

jldsave("example.jld2"; x, a=y, z)

# and if you want to confuse your future self and everyone else, do

jldsave("example.jld2"; z=x, x=y, y=z)

In the above examples, ; after the filename is important. Compression and non-default IO types may be set via positional arguments like:

jldopen("example.jld2", "w"; compress = true) do f

f["large_array"] = zeros(10000)

end

File interface

It is also possible to interact with JLD2 files using a file-like interface. The jldopen function accepts a file name and an argument specifying how the file should be opened:

using JLD2

f = jldopen("example.jld2", "r") # open read-only (default)

f = jldopen("example.jld2", "r+") # open read/write, failing if no file exists

f = jldopen("example.jld2", "w") # open read/write, overwriting existing file

f = jldopen("example.jld2", "a+") # open read/write, preserving contents of existing file or creating a new file

Data can be written to the file using write(f, "name", data) or f["name"] = data, or read from the file using read(f, "name") or f["name"]. When you are done with the file, remember to call close(f).

Like open, jldopen also accepts a function as the first argument, permitting do-block syntax:

jldopen("example.jld2", "w") do file

file["bigdata"] = randn(5)

end

Groups

It is possible to construct groups within a JLD2 file, which may or may not be useful for organizing your data. You can create groups explicitly:

jldopen("example.jld2", "w") do file

mygroup = JLD2.Group(file, "mygroup")

mygroup["mystuff"] = 42

end

or implicitly, by saving a variable with a name containing slashes as path delimiters:

jldopen("example.jld2", "w") do file

file["mygroup/mystuff"] = 42

end

# or save("example.jld2", "mygroup/mystuff", 42)

Both of these examples yield the same group structure, which you can see at the REPL:

julia> file = jldopen("example.jld2", "r")

JLDFile /Users/simon/example.jld2 (read-only)

└─ mygroup

└─ mystuff

Similarly, you can access groups directly:

jldopen("example.jld2", "r") do file

@assert file["mygroup"]["mystuff"] == 42

end

or using slashes as path delimiters:

@assert load("example.jld2", "mygroup/mystuff") == 42

Custom Serialization

The API is simple enough, to enable custom serialization for your type A you define

a new type e.g. ASerialization that contains the fields you want to store and define

JLD2.writeas(::Type{A}) = ASerialization.

Internally JLD2 will call Base.convert when writing and loading, so you need to make sure to extend that for your type.

struct A

x::Int

end

struct ASerialization

x::Vector{Int}

end

JLD2.writeas(::Type{A}) = ASerialization

Base.convert(::Type{ASerialization}, a::A) = ASerialization([a.x])

Base.convert(::Type{A}, a::ASerialization) = A(only(a.x))

If you do not want to overload Base.convert then you can also define

JLD2.wconvert(::Type{ASerialization}, a::A) = ASerialization([a.x])

JLD2.rconvert(::Type{A}, a::ASerialization) = A(only(a.x))

instead. This may be particularly relevant when types are involved that are not your own.

struct B

x::Float64

end

JLD2.writeas(::Type{B}) = Float64

JLD2.wconvert(::Type{Float64}, b::B) = b.x

JLD2.rconvert(::Type{B}, x::Float64) = B(x)

arr = [B(rand()) for i=1:10]

jldsave("test.jld2"; arr)

In this example JLD2 converts the array of B structs to a plain Vector{Float64} prior to

storing to disk.

Unpack.jl API

When additionally loading the UnPack.jl package, its @unpack and @pack! macros can be used to quickly save and load data from the file-like interface. Example:

using UnPack

file = jldopen("example.jld2", "w")

x, y = rand(2)

@pack! file = x, y # equivalent to file["x"] = x; file["y"] = y

@unpack x, y = file # equivalent to x = file["x"]; y = file["y"]

The group file_group = Group(file, "mygroup") can be accessed with the same file-like interface as the "full" struct.

Required Packages

FileIO

MacroTools

OrderedCollections

Reexport

Requires

TranscodingStreams

Used By Packages

AcuteBenchmark

AdditiveCellCom

AffineInvariantMCMC

Agents

AgentsPlots

AIBECS

AIControl

AlgorithmicRecourseDynamics

AlphaZero

Anatta

AndorSIF

Arena

ArgoData

AstroIC

AstroIO

AstroNbodySim

AstroPlot

AtomGraphs

AtomicGraphNets

Atomistic

AutoLandmarking

AutomationLabs

AutomationLabsDepot

AutomationLabsExportation

AvailablePotentialEnergyFramework

Bactos

BayesianNetworkRegression

Baytes

BaytesInference

BERT

BetaML

BIGUQ

BinaryBuilder

Bio

BioEnergeticFoodWebs

BioMakie

BioProfiling

BioStructures

BitSAD

BlobTracking

BlockDecomposition

Boltz

BOMBs

BoseWormPIMC

BranchFlowModel

BridgeLandmarks

BrkgaMpIpr

BrokenRecord

BSTModelKit

BundleAdjustmentModels

BundlerIO

Caesar

CalibrateEDMF

CanDecomp

CapacityExpansion

Causal

CausalForest

CausalInference

CDGRNs

CellSegmentation

ChainPlots

ChaoticEncryption

ChargeTransport

ChemfilesViewer

ClimaAtmos

ClimaLSM

ClimateERA

ClimateMachine

CMBLensing

CompHENS

ContactImplicitMPC

ContinuousTimePolicyGradients

CorticalSpectralTemporalResponses

CumulantsUpdates

DataAssimilationBenchmarks

DataFrameTools

Decapodes

DeepUnfoldedCDLMotif

DFControl

DFTforge

DPMMSubClusters

DrWatson

View all packages

Julia Packages

This website serves as a package browsing tool for the Julia programming language. It works by aggregating various sources on Github to help you find your next package.

By analogy, Julia Packages operates much like PyPI, Ember Observer, and Ruby Toolbox do for their respective stacks.

View Github

Julia教程8 读写文件操作_jld2 hdf-CSDN博客

>

Julia教程8 读写文件操作_jld2 hdf-CSDN博客

Julia教程8 读写文件操作

最新推荐文章于 2023-08-05 23:11:29 发布

张海军2013

最新推荐文章于 2023-08-05 23:11:29 发布

阅读量4.9k

收藏

13

点赞数

分类专栏:

Julia

文章标签:

Julia

Julia教程

Julia读写文件

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/zhanghaijun2013/article/details/88935267

版权

Julia

专栏收录该内容

14 篇文章

19 订阅

订阅专栏

文章目录

IO操作读文件操作写文件操作csv文件JLD2IOBuffer

在网易云课堂上直接搜索:Julia教程 ,就可以找到,教程的全名是:Julia教程 从入门到进阶

这是国内第一个免费的完整的Julia视频教程,非常适合Julia的入门。有兴趣的朋友可以去学习一下。

教程链接: Julia教程

文章目录

IO操作读文件操作写文件操作csv文件JLD2IOBuffer

IO操作

在对文件的操作中,如果我们未指定文件的路径,则默认的路径为

在使用Atom或vscode时,默认路径即为.jl文件的目录在使用jupyter notebook时,windows系统默认路径为’C:\Users\用户名’;MAC的目录在jupyter中可以看到

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c3piykXY-1592662419590)(https://raw.githubusercontent.com/Bounce00/pic/master/Julia%20course/Julia%20IO1.png)]

在使用REPL时,系统默认路径为Julia的安装目录,可以执行pwd()命令可查看当前的工作路径,,如果想更换路径,使用cd(dir::String)命令,若想跟换回原始路径,使用homedir()命令

读文件操作

s1 = readlines("sdy.txt")

s2 = open("sdy.txt","r")

line = readline(s2)

eof(s2)

position(s2)

read(s2,Char)

line = readline(s2)

read(s2,Int8)

close(s1)

在REPL中,可以查看open的用法,在open中,还可以调用函数

open(readline, "sdy.txt")

当然,也可以调用自定义的函数

readFunc(s::IOStream) = read(s, Char)

open(readFunc, "sdy.txt")

还可以像Python中的with open...as的用法一样打开文件

open("sdy.txt","r") do stream

for line in eachline(stream)

println(line)

end

end

也可以将上述过程定义成函数再用open操作

function readFunc2(stream)

for line in eachline(stream)

println(line)

end

end

open(readFunc2, "sdy.txt")

写文件操作

write("wrFile1.txt", "Hello World")

open("wrFile2.txt","w") do f

for ch in 'a':'z'

write(f, ch)

end

write(f,'\n')

end

open函数中的参数模式定义如下

ModeDescriptionrreadwwrite, create, truncateawrite, create, appendr+read, writew+read, write, create, truncatea+read, write, create, append

io = open("wrFile3.txt","w")

write(io, "Write File...\n")

但这种读写文件的方式对矩阵操作支持不太好

mat = reshape(1:9, (3,3))

write("mat.txt", mat)

最好使用DelimitedFiles包进行矩阵的读写操作

using DelimitedFiles

m = reshape(1:9, (3,3))

writedlm("number.txt", m, '\t')

cont = readdlm("number.txt")

除了txt格式外,Julia还可以对csv等格式,但Julia自带的数据序列化工具JLD2速度速度更快,扩平台,重点推荐。

csv文件

写csv文件与操作普通文件类似,中间加个逗号就可以了。

using DelimitedFiles

data = rand(Int8,10,5)

writedlm("file1.csv", data, ',')

读文件时,我们可以使用DataFrames来读csv文件。 关于DataFrames我们将在科学计算一节中讲到。

using DataFrames

df = readtable("file1.csv")

也可以使用CSV package自带的函数读取

using CSV

df1 = CSV.read("file1.csv")

用CSV.read()读上来的数据也是DataFrame格式。

JLD2

JLD2是JLD格式的改进,也是一种HDF5格式。

写文件

using JLD2

stri = "hello world"

fid = jldopen("jld_test.jld2","w")

write(fid, "stri", stri)

close(fid)

读文件

fid = jldopen("jld_test.jld2","r")

res = read(fid,"ss")

IOBuffer

相当于在内存中创建一个IOStream

io = IOBuffer()

write(io, "Hello World!", " JuliaLang is great!")

采用take!的方式将内容从IOBuffer中读出

String(take!(io))

如果在创建IOBuffer时就指定了里面的内容,则词IOBuffer为只读的。

io = IOBuffer("Hello World!")

write(io, "This is not writable.")

read(io, String)

关闭IOBuffer

close(io)

欢迎关注微信公众号:Quant_Times

优惠劵

张海军2013

关注

关注

0

点赞

13

收藏

觉得还不错?

一键收藏

知道了

0

评论

Julia教程8 读写文件操作

文章目录IO操作读文件操作写文件操作csv文件JLD2IOBuffer在网易云课堂上直接搜索:Julia教程 ,就可以找到,教程的全名是:Julia教程 从入门到进阶这是国内第一个免费的完整的Julia视频教程,非常适合Julia的入门。有兴趣的朋友可以去学习一下。教程链接:Julia教程欢迎关注微信公众号:Quant_TimesIO操作在对文件的操作中,如果我们未指定文件的路径,...

复制链接

扫一扫

专栏目录

signature=a20b3c3a6ff607accc831e6d492695f4,qfinance-token/yarn.lock at master · QFinanceDeFi/qfinanc...

weixin_35317737的博客

05-30

4万+

# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.# yarn lockfile v1"@babel/helper-module-imports@^7.12.1":version "7.12.5"resolved "https://registry.yarnpkg.com/@babel/helper-module-imp...

signature=10a018ba048c61ca6c010bf78a410268,qtumcore-node/yarn.lock at master · qtumproject/qtumcore-...

weixin_29097457的博客

05-29

2万+

# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.# yarn lockfile v1abbrev@1:version "1.1.1"resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987...

参与评论

您还未登录,请先

登录

后发表或查看评论

MAT.jl:用于读取MATLAB文件的Julia模块

02-03

MAT.jl:用于读取MATLAB文件的Julia模块

Jbd2:Hadoop

博客

03-15

1454

Jbd2:Hadoop1. 发展历史2. 主要特性3. 部分组件3.1 HDFS3.2 HBase3.3 Sqoop3.4 Zookeeper4. 实践操作4.1 创建Hadoop用户4.2 安装Java4.2.1 安装jdk4.2.2 修改环境变量4.2.3 SSH登录权限设置4.3 单机版Hadoop4.3.1 安装Hadoop4.3.2 修改系统环境变量4.3.3 修改hadoop-env.sh文件配置4.4 伪分布式Hadoop4.4.1 修改core-site.xml文件配置4.4.2 修改hdf

html登录页面整理

热门推荐

bible521125的专栏

01-19

51万+

3

function r()

{

var username=document.getElementById("username");

var pass=document.getElementById("password");

if(username.value=="")

{

alert("请输入用户名");

username.focus();

return;

}

if

Julia导入和导出数据(CSV、TXT、JLD)

YonminMa的博客

06-24

9152

文章目录CSV格式导出到CSV文件导入CSV文件TXT格式导出到TXT文件导入TXT文件JLD格式导出到JLD文件导入JLD文件

CSV格式

导出到CSV文件

在操作之前我们首先要安装DataFrames包和CSV包

Pkg.add("DataFrames")

Pkg.add("CSV")

创建一个DataFrame格式的数据df

using DataFrames

df = DataFrame(year = [1,2,3,3,3,3,3,3,54,5],name=["h","we","as","aa",

Julia 文件(File)读写

samLi0620的博客

08-05

220

从文件读取或者写入数据需要使用文件句柄。文件句柄其实就是一个指针,指针就是指向文件中的某个位置。从一个文件读取数据,应用程序首先要调用操作系统函数并传送文件名,并选一个到该文件的路径来打开文件,打开文件的函数取回一个顺序号,即文件句柄(file handle),该文件句柄对于打开的文件是唯一的识别依据。

Julia: readcsv和 readdlm

Julia & Rust & Python

12-02

1745

readcsv 和readdlm大家经常用到.

julia> path ="C:\\Users\\Administrator\\Desktop\\IF01.csv";julia> data1 =readcsv(path)

51828x8 Array{Float64,2}:readcsv=>julia> @time data1 =readcsv(path);

0.915586 seconds (1

Julia 将.jl脚本文件打包为可执行文件

QQVQQ的博客

04-06

2358

julia 打包文件为可执行程序

Julia simple tutorial

jackhuo2019的博客

12-31

145

x,y = y,x

g2 = x -> x^2

map(g2, [1,2,3,4])

(x->x^2) (2) # (2)是调用

g(x) = x^2 #simple fast defintion

函数小技巧

push!: 以!结尾的函数。!在函数名末尾,这是什么意思?更改或修改其输入的函数称为变异函数

逐点应用的功能,f.(x, y)和x .+ y(称为“广播”)

当我们添加一个.在函数名称之后,我们告诉Julia我们想要通过传递给函数的输入“ 广播 ”该函数。这意味着我们希望在输入上.

Feather.jl:用纯Julia读写羽毛文件

02-03

Julia库用于处理羽毛格式的文件 :warning: 项目状态 Feather.jl读取一种较旧的羽毛格式,现在称为“ Feather v1”。 当前的标准Feather v2只是写入磁盘的格式。 因此,您可能正在寻找 ,它将允许您读取和编写...

EDF.jl:在Julia中读写EDF文件

02-03

EDF.jl:在Julia中读写EDF文件

BundlerIO.jl:使用Julia读写Bundler文件

02-18

此Julia程序包将加载并保存,该在Motion的摄影测量和结构环境中使用。 安装 该软件包在Julia软件包存储库中可用,并且可以在Julia REPL软件包模式下安装: pkg > add FileIO BundlerIO 依赖项将自动安装。 该程序...

learn-julia:Julia教程

06-21

数字驱动世界的编程####Julia教程此存储库中的内容是文件。 要在不安装 Julia 的情况下运行代码,请登录并同步此存储库。 要在没有交互的情况下简单地查看笔记本,请转到

julia安装教程1

08-08

打开之后依次点击File --> Settings --> Install --> 搜索,之后依次安装 ink julia-client language-j

Julia视频教程

zhanghaijun2013的博客

02-28

9393

在网易云课堂上直接搜索:Julia教程 ,就可以找到,教程的全名是:Julia教程 从入门到进阶

这是国内第一个免费的完整的Julia视频教程,非常适合Julia的入门。有兴趣的朋友可以去学习一下。

教程链接:

Julia教程

...

Julia教程7 Julia绘图

zhanghaijun2013的博客

03-31

9042

绘图工具

官方推荐

官方推荐 Plots

简单的绘图

using Plots

y = rand(20,1)

plot(y,linewidth=2,title="My Plot")

Plots配合portfoliocomposition能够画出代码量少而且有内容丰富的图片,但在Julia v1.0版本中好像目前还不支持,等支持后会专门做一个用Plots绘图的教程。

快速绘图工具 GR

GR的...

Julia教程3 数学运算 矩阵运算

zhanghaijun2013的博客

03-30

3917

在网易云课堂上直接搜索:Julia教程 ,就可以找到,教程的全名是:Julia教程 从入门到进阶

这是国内第一个免费的完整的Julia视频教程,非常适合Julia的入门。有兴趣的朋友可以去学习一下。

教程链接:

Julia教程

数学运算

比Matlab更直观的数学表达方式

x = 10

2x

>>20

但这就导致了可能会出现语法的冲突

十六进制整数文本表达式 0xff 可以被解析...

vscode julia安装教程

最新发布

09-02

当然!下面是在VS Code中安装Julia的步骤:

1. 首先,确保你已经安装了 Visual Studio Code(VS Code)。如果没有,请先下载并安装它。

2. 打开VS Code,并在左侧的扩展面板中搜索 "Julia"。点击安装 "Julia" 扩展。

3. 安装完成后,你将看到一个 "Julia" 图标出现在左侧的活动栏中。点击它以打开 Julia 面板。

4. 在 Julia 面板中,点击 "download" 下载并安装 Julia 解释器。根据你的操作系统,选择对应的版本。

5. 安装完成后,你可以在 Julia 面板中选择 "Julia解释器路径",然后指定 Julia 解释器的路径。如果你已经将 Julia 添加到系统路径中,那么可以选择 "自动检测"。

6. 完成上述步骤后,你就可以在 VS Code 中使用 Julia 了。可以创建新的 Julia 文件,编写代码,并使用 VS Code 提供的功能进行编辑和调试。

希望以上步骤对你有帮助!如果你有其他问题,请随时提问。

“相关推荐”对你有帮助么?

非常没帮助

没帮助

一般

有帮助

非常有帮助

提交

张海军2013

CSDN认证博客专家

CSDN认证企业博客

码龄12年

企业员工

125

原创

1万+

周排名

4913

总排名

43万+

访问

等级

4874

积分

1652

粉丝

516

获赞

117

评论

3939

收藏

私信

关注

热门文章

什么是噪声温度?-174dBm/Hz又是什么?

24791

策略篇--量化投资之双均线策略

17196

Matlab中短时傅里叶变换 spectrogram和stft的用法

16200

零点和极点到底影响了什么?什么是最小相位系统?

14834

Vivado生成HDL例化模板

12662

分类专栏

UVM

1篇

git

2篇

语音识别

2篇

自动驾驶

2篇

GPU

3篇

笔记

markdown

1篇

python

20篇

Julia

14篇

Quant

1篇

MachineLearning

1篇

C++

5篇

matlab

5篇

FPGA

51篇

信号处理

12篇

最新评论

UVM手把手教程系列(二)Phase机制介绍

CSDN-Ada助手:

不知道 Java 技能树是否可以帮到你:https://edu.csdn.net/skill/java?utm_source=AI_act_java

Xilinx FPGA Partial Reconfiguration 部分重配置 详细教程

Rachel_Amber:

请问您成功实现重配置功能了吗

11 FPGA时序约束实战篇之伪路径约束

水王汪:

您这个图片我怎么看不了,请问一下博主,就是在跨时钟域是否都需要进行伪路径约束,还有就是,set-clokc-group和set-false-path是不是一样的呀

Vivado中模块封装成edif和dcp

路飞胡:

请问这种dcp文件是自已写的还是vivado自带的ip核呀

Hold Time违例,该如何解决

m0_46577367:

我试过插两个反相器,应该是可以的,就是需要加don't touch,防止被优化

最新文章

UVM手把手教程系列(二)Phase机制介绍

UVM手把手教程系列(一)UVM基础

经过BUFGMUX的时钟该如何约束

2024年2篇

2023年9篇

2022年5篇

2021年23篇

2020年52篇

2019年34篇

目录

目录

分类专栏

UVM

1篇

git

2篇

语音识别

2篇

自动驾驶

2篇

GPU

3篇

笔记

markdown

1篇

python

20篇

Julia

14篇

Quant

1篇

MachineLearning

1篇

C++

5篇

matlab

5篇

FPGA

51篇

信号处理

12篇

目录

评论

被折叠的  条评论

为什么被折叠?

到【灌水乐园】发言

查看更多评论

添加红包

祝福语

请填写红包祝福语或标题

红包数量

红包个数最小为10个

红包总金额

红包金额最低5元

余额支付

当前余额3.43元

前往充值 >

需支付:10.00元

取消

确定

下一步

知道了

成就一亿技术人!

领取后你会自动成为博主和红包主的粉丝

规则

hope_wisdom 发出的红包

实付元

使用余额支付

点击重新获取

扫码支付

钱包余额

0

抵扣说明:

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

余额充值

GitHub - JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia

GitHub - JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia

Skip to content

Toggle navigation

Sign in

Product

Actions

Automate any workflow

Packages

Host and manage packages

Security

Find and fix vulnerabilities

Codespaces

Instant dev environments

Copilot

Write better code with AI

Code review

Manage code changes

Issues

Plan and track work

Discussions

Collaborate outside of code

Explore

All features

Documentation

GitHub Skills

Blog

Solutions

For

Enterprise

Teams

Startups

Education

By Solution

CI/CD & Automation

DevOps

DevSecOps

Resources

Learning Pathways

White papers, Ebooks, Webinars

Customer Stories

Partners

Open Source

GitHub Sponsors

Fund open source developers

The ReadME Project

GitHub community articles

Repositories

Topics

Trending

Collections

Pricing

Search or jump to...

Search code, repositories, users, issues, pull requests...

Search

Clear

Search syntax tips

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

Sign up

You signed in with another tab or window. Reload to refresh your session.

You signed out in another tab or window. Reload to refresh your session.

You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

JuliaIO

/

JLD2.jl

Public

Notifications

Fork

82

Star

512

HDF5-compatible file format in pure Julia

License

View license

512

stars

82

forks

Branches

Tags

Activity

Star

Notifications

Code

Issues

24

Pull requests

7

Actions

Projects

0

Wiki

Security

Insights

Additional navigation options

Code

Issues

Pull requests

Actions

Projects

Wiki

Security

Insights

JuliaIO/JLD2.jl

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

 masterBranchesTagsGo to fileCodeFolders and filesNameNameLast commit messageLast commit dateLatest commit History609 Commits.github.github  benchmarkbenchmark  docsdocs  extext  srcsrc  testtest  .gitignore.gitignore  CHANGELOG.mdCHANGELOG.md  LICENSE.mdLICENSE.md  Project.tomlProject.toml  README.mdREADME.md  View all filesRepository files navigationREADMELicenseJLD2

Documentation

Tests

CodeCov

Downloads

JLD2 saves and loads Julia data structures in a format comprising a subset of HDF5, without any dependency on the HDF5 C library.

JLD2 is able to read most HDF5 files created by other HDF5 implementations supporting HDF5 File Format Specification Version 3.0 (i.e. libhdf5 1.10 or later) and similarly those should be able to read the files that JLD2 produces. JLD2 provides read-only support for files created with the JLD package.

Reading and writing data

save and load functions

The save and load functions, provided by FileIO, provide a mechanism to read and write data from a JLD2 file. To use these functions, you may either write using FileIO or using JLD2. FileIO will determine the correct package automatically.

The save function accepts an AbstractDict yielding the key/value pairs, where the key is a string representing the name of the dataset and the value represents its contents:

using FileIO

save("example.jld2", Dict("hello" => "world", "foo" => :bar))

The save function can also accept the dataset names and contents as arguments:

save("example.jld2", "hello", "world", "foo", :bar)

When using the save function, the file extension must be .jld2, since the extension .jld currently belongs to the previous JLD package.

If called with a filename argument only, the load function loads all datasets from the given file into a Dict:

load("example.jld2") # -> Dict{String,Any}("hello" => "world", "foo" => :bar)

If called with a single dataset name, load returns the contents of that dataset from the file:

load("example.jld2", "hello") # -> "world"

If called with multiple dataset names, load returns the contents of the given datasets as a tuple:

load("example.jld2", "hello", "foo") # -> ("world", :bar)

A new interface: jldsave

jldsave makes use of julia's keyword argument syntax to store files,

thus leveraging the parser and not having to rely on macros. The new interface can be imported with using JLD2. To use it, write

using JLD2

x = 1

y = 2

z = 42

# The simplest case:

jldsave("example.jld2"; x, y, z)

# it is equivalent to

jldsave("example.jld2"; x=x, y=y, z=z)

# You can assign new names selectively

jldsave("example.jld2"; x, a=y, z)

# and if you want to confuse your future self and everyone else, do

jldsave("example.jld2"; z=x, x=y, y=z)

In the above examples, ; after the filename is important. Compression and non-default IO types may be set via positional arguments like:

jldopen("example.jld2", "w"; compress = true) do f

f["large_array"] = zeros(10000)

end

File interface

It is also possible to interact with JLD2 files using a file-like interface. The jldopen function accepts a file name and an argument specifying how the file should be opened:

using JLD2

f = jldopen("example.jld2", "r") # open read-only (default)

f = jldopen("example.jld2", "r+") # open read/write, failing if no file exists

f = jldopen("example.jld2", "w") # open read/write, overwriting existing file

f = jldopen("example.jld2", "a+") # open read/write, preserving contents of existing file or creating a new file

Data can be written to the file using write(f, "name", data) or f["name"] = data, or read from the file using read(f, "name") or f["name"]. When you are done with the file, remember to call close(f).

Like open, jldopen also accepts a function as the first argument, permitting do-block syntax:

jldopen("example.jld2", "w") do file

file["bigdata"] = randn(5)

end

Groups

It is possible to construct groups within a JLD2 file, which may or may not be useful for organizing your data. You can create groups explicitly:

jldopen("example.jld2", "w") do file

mygroup = JLD2.Group(file, "mygroup")

mygroup["mystuff"] = 42

end

or implicitly, by saving a variable with a name containing slashes as path delimiters:

jldopen("example.jld2", "w") do file

file["mygroup/mystuff"] = 42

end

# or save("example.jld2", "mygroup/mystuff", 42)

Both of these examples yield the same group structure, which you can see at the REPL:

julia> file = jldopen("example.jld2", "r")

JLDFile /Users/simon/example.jld2 (read-only)

└─ mygroup

└─ mystuff

Similarly, you can access groups directly:

jldopen("example.jld2", "r") do file

@assert file["mygroup"]["mystuff"] == 42

end

or using slashes as path delimiters:

@assert load("example.jld2", "mygroup/mystuff") == 42

When loading files with nested groups these will be unrolled into paths by default but

yield nested dictionaries but with the nested keyword argument.

load("example.jld2") # -> Dict("mygroup/mystuff" => 42)

load("example.jld2"; nested=true) # -> Dict("mygroup" => Dict("mystuff" => 42))

Custom Serialization

The API is simple enough, to enable custom serialization for your type A you define

a new type e.g. ASerialization that contains the fields you want to store and define

JLD2.writeas(::Type{A}) = ASerialization.

Internally JLD2 will call Base.convert when writing and loading, so you need to make sure to extend that for your type.

struct A

x::Int

end

struct ASerialization

x::Vector{Int}

end

JLD2.writeas(::Type{A}) = ASerialization

Base.convert(::Type{ASerialization}, a::A) = ASerialization([a.x])

Base.convert(::Type{A}, a::ASerialization) = A(only(a.x))

If you do not want to overload Base.convert then you can also define

JLD2.wconvert(::Type{ASerialization}, a::A) = ASerialization([a.x])

JLD2.rconvert(::Type{A}, a::ASerialization) = A(only(a.x))

instead. This may be particularly relevant when types are involved that are not your own.

struct B

x::Float64

end

JLD2.writeas(::Type{B}) = Float64

JLD2.wconvert(::Type{Float64}, b::B) = b.x

JLD2.rconvert(::Type{B}, x::Float64) = B(x)

arr = [B(rand()) for i=1:10]

jldsave("test.jld2"; arr)

In this example JLD2 converts the array of B structs to a plain Vector{Float64} prior to

storing to disk.

Unpack.jl API

When additionally loading the UnPack.jl package, its @unpack and @pack! macros can be used to quickly save and load data from the file-like interface. Example:

using UnPack

file = jldopen("example.jld2", "w")

x, y = rand(2)

@pack! file = x, y # equivalent to file["x"] = x; file["y"] = y

@unpack x, y = file # equivalent to x = file["x"]; y = file["y"]

The group file_group = Group(file, "mygroup") can be accessed with the same file-like interface as the "full" struct.

About

HDF5-compatible file format in pure Julia

Topics

julia

julia-language

jld2

hdf5-implementations

Resources

Readme

License

View license

Activity

Custom properties

Stars

512

stars

Watchers

13

watching

Forks

82

forks

Report repository

Releases

76

v0.4.46

Latest

Feb 16, 2024

+ 75 releases

Packages

0

No packages published

Contributors

65

+ 51 contributors

Languages

Julia

100.0%

Footer

© 2024 GitHub, Inc.

Footer navigation

Terms

Privacy

Security

Status

Docs

Contact

Manage cookies

Do not share my personal information

You can’t perform that action at this time.