Ruby简单读取文件

用于获取键盘输入,并输出

 

while line = gets
    puts line
end

 

 按照行读取文件内容

 

File.open("E:/workspaceNew/RubyStudy/test.txt") do |file|
file.each_line{|line| puts "Got #{line.dump}"}
file.close();
end

 

 按照行读取文件,并按照制定的字符串进行分割.本文中以e字母进行分割

 

File.open("E:/workspaceNew/RubyStudy/test.txt") do |file|
file.each_line("e"){|line| puts "Got #{line.dump}"}
file.close();
end

 

 使用IO.foreach读取文件

 

IO.foreach("E:/workspaceNew/RubyStudy/test.txt"){|line| puts line};

 

 可以将读取的文件保存在一个字符串当中

 

str = IO.read("E:/workspaceNew/RubyStudy/test.txt");
  puts str.length;
  puts str[0,30]

 

 也可以将读取的文件保存在一个数组当中

 

arr = IO.readlines("E:/workspaceNew/RubyStudy/test.txt")
puts arr.length;
puts arr[0]

 

 

ruby on rails读取excel文件完整流程 ruby on rails,前端上传Excel文件,后端读取文件内容并写入数据库。 阅读详情

相关推荐

Ruby IO操作:读取和写入文件的完全指南

通过上述步骤,您可以使用Ruby的IO类来轻松地进行文件的读取和写入操作。记得在操作完成后关闭文件,以确保代码的健壮性和资源的释放。希望本文能对您有所帮助!在Ruby中,IO类提供了强大的功能,可以用于读取和写入文件。本篇文章将详细介绍如何使用Ruby进行文件的读取和写入操作。为了确保文件在任何情况下都能被关闭,可以使用块形式来打开文件。当块执行结束时,文件会自动关闭,无需手动调用close方法。

YurwRuby的博客 502

ruby打开文件的不同模式

在 Ruby 中,打开文件时可以使用不同的模式来指定文件的读写方式。

Toml_的博客 660

Ruby读写txt文件

Ruby 读写 txt文件 自用 网络资源...

Ruby学习之文件的写入和读取

Ruby 提供了一整套 I/O 相关的方法,在内核(Kernel)模块中实现,所有的 I/O 方法派生自 IO 类,类 IO 提供了所有基础的方法,比如 read、 write、 gets、 puts、 readline、 getc 和 printf,我们接下来就来分别看下这些个函数。 首先来看一下输出打印信息的函数。 puts 语句指示程序显示存储在变量中的值,这将在每行末尾添加一个新行,来...

luyaran的博客 8770

恶意流量检测数据集

对当前公开的恶意流量相关数据集进行归类总结,持续更新相关论文

tyutwuxuan的博客 1940

麒麟和UOS系统编写桌面点击可执行文件

在桌面上创建一个快捷方式(.desktop 文件)来运行脚本。打开文本编辑器,创建一个新文件,如。这可以通过文本编辑器来完成。首先,确保您的脚本具有执行权限。现在,您应该可以双击桌面上的。保存此文件到桌面目录,例如,在桌面上创建一个快捷方式(文件来运行您的脚本了。使用图形界面或命令行给。现在您可以双击桌面上的。为您脚本的实际路径。是您脚本的实际路径。文件来运行您的脚本了。

u013934107的博客 7250

使用Ruby读取excel文件的简单例子

本篇文章已经比较老,全新的导入导出处理见 这里 Rails 的 Wiki 上有講到幾個方法可以直接用 Ruby 寫 Excel file:1. office 2003 之後有支援以 xml 做為儲存格式。只要符合schema 的 XML file 就會被正確解讀成 Excel file。這樣產生出來的文件只有 office 2003 以後才能讀取2. 用 Jakarta POI  的 Ruby ...

horace_lee的专栏 508

HFSS螺旋线圈的优化设计与高效仿真实践

本文详细介绍了HFSS螺旋线圈的优化设计与高效仿真实践,涵盖核心参数设置、参数化建模、仿真加速技巧及结果分析。通过HFSS软件实现螺旋线圈的高效仿真,特别适用于无线充电和RFID系统设计,提升设计效率与性能验证准确性。

weixin_30235225的博客 445

ruby中excel简单操作以及文件读取操作方法

# -*-coding:utf-8 -*-#author:kanlijunrequire 'win32ole'require 'fileutils'class ResultAnalyse @@i=0 @@f=0 def initialize path excel =WIN32OLE.new('excel.application') @workbook =excel.WorkBoo...

weixin_30505485的博客 264

Eclipse 数据空间组件(EDC)项目介绍

在当今数字经济的背景下,数据成为了新的“石油”。但与石油不同的是,数据可以无限共享且再生。然而,如何在保持数据主权的前提下实现高效、安全的数据共享,成为了企业和组织面临的主要挑战。这时,**Eclipse 数据空间组件(Eclipse Dataspace Components, EDC)**应运而生,它为企业在多方参与的数据交换中提供了一个标准化、可扩展的解决方案。数据空间不仅是一个多组织间的数据共享协议,还是一套支撑这些协议运行的技术基础设施。

m0_74331272的博客 4599

浪潮服务器ESXI6.7部署实战:手动集成RAID驱动避坑指南

本文详细介绍了在浪潮服务器上部署ESXI6.7时手动集成RAID驱动的完整流程和避坑指南。从驱动获取、解压技巧到封装实战,逐步解决国产服务器常见的驱动缺失问题,特别针对PM8060 RAID卡提供具体解决方案,帮助IT运维人员顺利完成ESXI部署。

weixin_33725722的博客 461

ruby读取注册表判断文件选项中的使用简单文件共享复选框是否选中

#功能描述:用来判断文件选项中的使用 # 简单文件共享复选框是否选中 ## #参数说明:无 # #返回值 :返回选中与不选中时值:选中为41、没有选中为 40 def judgeCheckBox() Win32::Registry::HKEY_LOCAL_MACHINE.open('SYSTEM\CurrentControlSet\Control\Lsa')...

dk19870616的博客 326

一篇把 Transport 做到可复制的实战指南:从 CTS 底层表到 LSMW、Variant、Layout、TABU、Query、SAPscript 全对象搬运

本文深入解析SAP系统中的Transport机制,将其视为可审计、可追溯的变更流水线。文章从CTS/Transport Organizer通用方法论和具体对象搬运手法两个层面展开,详细介绍了LSMW项目、程序Variant、屏幕/ALV Layout、表数据、ABAP Query、SAPscript和SO10标准文本等对象的传输策略。通过真实案例说明,代码对象往往能被正确传输,但"运行体验"和"业务配置"却常被遗漏,导致QAS测试正常而PRD环境异常。文章提供了一套可

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 173

使用ruby读取文件,爽的不得了

[code="Ruby"]=begin 使用ruby读取指定路径的文件 =end fileName = ARGV[0] #获取从控制台输入的文件路径 file = open(fileName) #打开文件 while text = file.gets do #使用while每循环一次,输出一行字符串 print text #向控制台打印内容 end #当text为nil时就结束循...

troms的博客 260

ICM20948 DMP代码详解(54)

ICM20948 DMP代码详解(54)

phmatthaus的专栏 424

最佳RANSAC算法拟合矩形的实现(基于PCL库)

最佳RANSAC算法是一种常用的参数估计方法,用于从数据集中拟合出最佳的模型。在计算机视觉和图像处理领域,最佳RANSAC算法常用于拟合几何形状,如矩形。本文将详细介绍如何使用点云库(PCL)中的最佳RANSAC算法来拟合矩形,并提供相应的源代码。

JbPatch的博客 1542

Ruby读取二进制文件

贴上一段代码,用Ruby读取BMP图像的,以作备忘。其中,sysread方法中的参数表示读取的字节数。bmp_read_file = File.open("E://test_example//C++.bmp","rb") write_file = File.new("E://test_example//target","wb") while true begin

惊叹号 1970

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ruby读取yaml文件

假如我们有一个写好的yaml文件:   program:   id: 1   input: 1 2   output: 3   注意:":" 后面必须有一个空格, 层级关系用2个空格     读取方式: require 'yaml'problem = YAML.load(File.open("a.yml"))puts "problem id is #{problem["pr...

素年小事 1158

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Ruby语言的文件操作

通过以上内容,我们了解了Ruby语言的文件操作的基本概念、常见方法和实际应用。这些功能使得Ruby在处理文件相关任务时非常高效和灵活。通过合理的错误处理、文件遍历和权限管理,开发者可以编写出稳定、高效的文件处理程序。希望本文能够帮助你更深入地理解Ruby的文件操作,利用这些知识来提升你的编程能力。

2501_90932908的博客 1589

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

ruby文件_Ruby文件处理

ruby文件 Ruby文件处理 (Ruby File Handling) In this tutorial we will learn about File handling in Ruby programming language. We will learn how to read data from a file, write data into a file, append data i...

cunfen6312的博客 318

使用ruby读取文件

[code="ruby"]=begin 使用ruby读取指定路径的文件 =end fileName = ARGV[0] #获取从控制台输入的文件路径 file = open(fileName) #打开文件 while text = file.gets do #使用while每循环一次,输出一行字符串 print text #向控制台打印内容 end #当text为nil时就结束循...

Tang Ji Zhong 337

ruby 查看csv文件_如何使用CSV和Ruby构建终端游戏

ruby 查看csv文件by Andrew Bales 通过安德鲁·巴尔斯 如何使用CSV和Ruby构建终端游戏 (How you can build a terminal game with CSV and Ruby) In this article, you’ll learn how to build a terminal game with a CSV and a few Ruby ge...

cumi6497的博客 346

ruby 语法(ARGV,传参数)--读取文件档案

脚本读取的文字档案: ex15_sample.txt This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. ruby脚本: filename = ARGV.first # ARGV获取传进来第一个的参数 prompt

maxiaokun55的专栏 6953

Ruby 文件处理

#r read, #w write, #a append, #r+ 读写方式 从文件的头位置开始读取或写入, #w+ 读写方式,如果文件已存在清空该文件,不存在就创建一个新的文件, #a+ 如果文件存在就在最后面附加,如果不存在就创建一个新文件。 FileName="newfile.txt" file=File.open(FileName,'a') file.puts '...

weixin_30488085的博客 130
上一篇: 初涉ajax
下一篇: Beginning Ruby
refactaar
博客等级 码龄17年 0粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值