- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个大文件,其中的“部分”由 ***
分割。我必须为每个部分创建一个散列和一个新文件,其中每个部分都以新格式编写(我可能会为部分编写多个文件)。每个部分都需要独特的逻辑才能转换为散列(由“”、或“\n”、或“=”或“/some patter/”分隔)。
我正在寻找一种方法来识别部分并应用适当的逻辑将部分转换为散列。我可以编写单独的逻辑片段,但它们是基于模式调用的单独方法还是类?
由于文件很大,我试图逐行读取、操作和写入。我已经看到了在部分之间使用 glob 线的方法,但并不特别关心这种类型的解决方案。我对如何捕获各部分之间的线条并在适当的时候逐行应用不同的逻辑有点困惑。
任何方向表示赞赏。谢谢!
这是一些输入文件:
*** Summary ***
Job Name = test Date created: Mon Jan 14 15:48:33 2013
*** Analysis Information
Steady State is ON
Turbulent Incompressible Flow is ON
Static Temperature Equation is ON
Mixed Convection is ON
*** Field Variable Results Summary For Iteration 300
Var Mean at Max at Min
Vx Vel +5.71519e+002 1320103 +3.02718e+004 1319857 -2.66582e+004 mm/s
Vy Vel +3.40035e+002 158922 +2.79257e+004 1319731 -1.42855e+004 mm/s
Vz Vel -7.17959e+002 1318038 +1.62986e+004 1319053 -2.21582e+004 mm/s
Press -2.05980e+001 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2
Temp +4.60000e+001 10965 +4.60000e+001 315867 +4.60000e+001 C
TurbK +1.19616e+006 1319490 +1.44421e+008 10966 +1.81700e-008 mm^2/s^2
TurbD +1.71412e+009 1319490 +2.88554e+011 233065 +5.37798e-004 mm^2/s^3
Scal1 +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000
PTotl -5.91285e+000 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2
EVisc +2.52037e-004 1320370 +1.14488e-002 2229 +0.00000e+000 g/mm-s
ECond +1.05355e-002 1352833 +5.88890e-002 2229 +0.00000e+000 W/mm-K
Dens +2.34793e-004 58024 +3.43080e-003 315867 +1.20473e-006 g/mm^3
Visc +1.62605e-005 10965 +1.81700e-005 2229 +0.00000e+000 g/mm-s
Cond +2.50840e-002 2229 +2.04000e-001 315867 +2.56300e-005 W/mm-K
SpecH +1.01202e+000 38432 +1.81000e+000 10249 +1.00500e-003 J/g-K
Emiss +8.94911e-001 10965 +1.00000e+000 2229 +0.00000e+000
Transmiss +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000
WRough +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000 mm
SeeBeck +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000 V/K
GenT +1.11977e+003 223286 +1.18027e+005 584515 +3.19558e-013 1/s
*** Openings ***
*** Outlet 1 ***
Surface ID = 2329
Node near Minimum X,Y,Z of opening = 11761
Minimum X,Y,Z of opening = 369.964000, 11.275438, -98.433898
Mass Flow Out = -1.55703 g/s
Volume Flow Out = -1.29242e+006 mm^3/s
Reynolds Number = 1303.45
Outlet Bulk Pressure = -0 N/m^2
Outlet Bulk Temperature = 46 C
Outlet Mach Number = 0.00734951
*** Outlet 2 ***
Surface ID = 2332
Node near Minimum X,Y,Z of opening = 11125
Minimum X,Y,Z of opening = 369.964000, 73.727289, -114.615876
Mass Flow Out = -20.4612 g/s
Volume Flow Out = -1.6984e+007 mm^3/s
Reynolds Number = 11182.5
Outlet Bulk Pressure = -0 N/m^2
Outlet Bulk Temperature = 46 C
Outlet Mach Number = 0.0079087
*** Outlet 3 ***
Surface ID = 2335
Node near Minimum X,Y,Z of opening = 10924
Minimum X,Y,Z of opening = 369.964000, 164.751344, 40.640056
Mass Flow Out = -32.8714 g/s
Volume Flow Out = -2.72852e+007 mm^3/s
Reynolds Number = 17965
Outlet Bulk Pressure = -0 N/m^2
Outlet Bulk Temperature = 46 C
Outlet Mach Number = 0.00750077
*** Fluid Energy Balance Information:
MdotIn x Cp x (TOut - TIn) = 663.69 Watts
(Numerical) Energy Out - Energy In = 0.36447 Watts
Heat Transfer from Wall To Fluid = 761.35 Watts
Heat Transfer Due to Sources In Fluid = 0 Watts
*** Solid Energy Balance Information:
Heat Transfer from Exterior To Solid = 0 Watts
Heat Transfer Due to Sources In Solid = 761 Watts
Heat Transfer From Fluid To Solid = -761.31 Watts
*** Sum of Fluid Forces on Walls ***
ShearX, PressX = 68651 78199 microNewtons
ShearY, PressY = 39030 6.9349e+006 microNewtons
ShearZ, PressZ = -19749 -4.1017e+006 microNewtons
*** Data for internal fans
Fan Part Id = 16 Fan Name = fname1
Operating Pressure Rise = 0.46945 Inches of Water
Operating FlowRate = 36.0109 CFM
Fan Part Id = 94 Fan Name = fname2
Operating Pressure Rise = 0.309645 Inches of Water
Operating FlowRate = 2.33407 CFM
Fan Part Id = 95 Fan Name = fname3
Operating Pressure Rise = 0.267133 Inches of Water
Operating FlowRate = 8.78264 CFM
*** Analysis Statistics:
Input: 461 seconds
Analysis: 12686 seconds
Output: 179 seconds
Total: 13326 seconds
到目前为止,这是我所拥有的:
sum_file = File.new('sum_file.sum', 'r')
sum_file_hashed = File.new('sum_file_hashed', 'w')
inSection = false #flag when in or out of a section?
while (line = sum_file.gets ) #while reading lines
case line
when /\*{3}/ #Found Sections by ***
inSection = true #in a section
l = line.gsub('*', '').strip
sum_file_hashed.puts('Found a section: ' + l ) #write section name
end
### I'm not sure how to introduce specific logic when in a certain type of section ###
end
sum_file.close
sum_file_hashed.close
我现在正在尝试类似的事情:
while /found section/
if /match pattern a/
call parsera
if /match pattern b/
call parserb
end
parsera
while =! /a section/
do stuff
return?
最佳答案
Ruby 的 Enumerable 包含 slice_before
这对于此类任务非常有用,它可以根据某些标记将文件分解为多个 block 。
require 'pp'
blocks = DATA.readlines.map(&:strip).reject{ |l| l == '' }.slice_before(/\A\*{3}/)
pp blocks.to_a
__END__
*** Summary ***
Job Name = test Date created: Mon Jan 14 15:48:33 2013
*** Analysis Information
Steady State is ON
Turbulent Incompressible Flow is ON
Static Temperature Equation is ON
Mixed Convection is ON
*** Field Variable Results Summary For Iteration 300
Var Mean at Max at Min
Vx Vel +5.71519e+002 1320103 +3.02718e+004 1319857 -2.66582e+004 mm/s
Vy Vel +3.40035e+002 158922 +2.79257e+004 1319731 -1.42855e+004 mm/s
Vz Vel -7.17959e+002 1318038 +1.62986e+004 1319053 -2.21582e+004 mm/s
Press -2.05980e+001 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2
Temp +4.60000e+001 10965 +4.60000e+001 315867 +4.60000e+001 C
TurbK +1.19616e+006 1319490 +1.44421e+008 10966 +1.81700e-008 mm^2/s^2
TurbD +1.71412e+009 1319490 +2.88554e+011 233065 +5.37798e-004 mm^2/s^3
Scal1 +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000
PTotl -5.91285e+000 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2
EVisc +2.52037e-004 1320370 +1.14488e-002 2229 +0.00000e+000 g/mm-s
ECond +1.05355e-002 1352833 +5.88890e-002 2229 +0.00000e+000 W/mm-K
Dens +2.34793e-004 58024 +3.43080e-003 315867 +1.20473e-006 g/mm^3
Visc +1.62605e-005 10965 +1.81700e-005 2229 +0.00000e+000 g/mm-s
Cond +2.50840e-002 2229 +2.04000e-001 315867 +2.56300e-005 W/mm-K
SpecH +1.01202e+000 38432 +1.81000e+000 10249 +1.00500e-003 J/g-K
Emiss +8.94911e-001 10965 +1.00000e+000 2229 +0.00000e+000
Transmiss +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000
WRough +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000 mm
SeeBeck +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000 V/K
GenT +1.11977e+003 223286 +1.18027e+005 584515 +3.19558e-013 1/s
我缩短了数据,因为样本太多了。
运行代码输出:
[["*** Summary ***", "Job Name = test Date created: Mon Jan 14 15:48:33 2013"], ["*** Analysis Information", "Steady State is ON", "Turbulent Incompressible Flow is ON", "Static Temperature Equation is ON", "Mixed Convection is ON"], ["*** Field Variable Results Summary For Iteration 300", "Var Mean at Max at Min", "Vx Vel +5.71519e+002 1320103 +3.02718e+004 1319857 -2.66582e+004 mm/s", "Vy Vel +3.40035e+002 158922 +2.79257e+004 1319731 -1.42855e+004 mm/s", "Vz Vel -7.17959e+002 1318038 +1.62986e+004 1319053 -2.21582e+004 mm/s", "Press -2.05980e+001 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2", "Temp +4.60000e+001 10965 +4.60000e+001 315867 +4.60000e+001 C", "TurbK +1.19616e+006 1319490 +1.44421e+008 10966 +1.81700e-008 mm^2/s^2", "TurbD +1.71412e+009 1319490 +2.88554e+011 233065 +5.37798e-004 mm^2/s^3", "Scal1 +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000", "PTotl -5.91285e+000 50858 +5.19412e+003 50905 -1.44865e+003 N/m^2", "EVisc +2.52037e-004 1320370 +1.14488e-002 2229 +0.00000e+000 g/mm-s", "ECond +1.05355e-002 1352833 +5.88890e-002 2229 +0.00000e+000 W/mm-K", "Dens +2.34793e-004 58024 +3.43080e-003 315867 +1.20473e-006 g/mm^3", "Visc +1.62605e-005 10965 +1.81700e-005 2229 +0.00000e+000 g/mm-s", "Cond +2.50840e-002 2229 +2.04000e-001 315867 +2.56300e-005 W/mm-K", "SpecH +1.01202e+000 38432 +1.81000e+000 10249 +1.00500e-003 J/g-K", "Emiss +8.94911e-001 10965 +1.00000e+000 2229 +0.00000e+000", "Transmiss +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000", "WRough +0.00000e+000 10965 +0.00000e+000 315867 +0.00000e+000 mm", "SeeBeck +0.00000e+000 0 +0.00000e+000 0 +0.00000e+000 V/K", "GenT +1.11977e+003 223286 +1.18027e+005 584515 +3.19558e-013 1/s"]]
The file was converted to an array of arrays. Leading and trailing whitespace, along with new-lines and carriage-returns were stripped, and blank lines were removed.
Processing the file further is done using a loop over the outer array, and a test that looks at the first line of each sub-array to determine what to do with that block. Something like this would be a starting point:
hash = {}
blocks.each do |block|
case block.shift
when /Summary/
# process the summary information
when /Analysis Information/
# process the analysis information
hash[:analysis_information] = Hash[block.map{ |r| r.split(/ +is +/) }]
when /Field Variable Results/
# process the field variable results
end
end
想法是,当代码完成后,hash
将在哈希的哈希或数组的哈希中包含已解析的数据,以供您将其写出。我建议考虑使用 YAML,因为它可以简化将数据序列化到文件的工作。
我不打算添加更多内容,因为这个问题听起来很像家庭作业,而且解析这些行也不是那么难。将文件分成 block 是一项更大的任务,这部分已经为您完成了。
关于ruby - 根据文件中的不同部分从独特的逻辑中生成 ruby 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14531337/
以下是一个非常简单的ruby服务器。 require 'socket' local_socket = Socket.new(:INET, :STREAM) local_addr = Socket.
我正在使用 OS X(使用 bash),并且是 unix 的新手。我想知道是否可以修改一些文件以便运行 ruby 程序,我不需要“ruby file.rb”,而是可以运行“ruby.rb”。 有理
我在用 Ruby 替换字符串时遇到一些问题。 我的原文:人之所为不如兽之所为。 我想替换为:==What== human does is not like ==what== animal does.
我想在一个循环中从 Ruby 脚本做这样的事情: 写一个文件a.rb(每次迭代都会改变) 执行系统(ruby 'a.rb') a.rb 将带有结果的字符串写入文件“results” a.rb 完成并且
我的问题是尝试创建一个本地服务器,以便我可以理解由我的新团队开发的应用程序。我的问题是我使用的是 Ruby 2.3.3,而 Gemfile 需要 2.3.1。我无法编辑 Gemfile,因为我被告知很
我有一个使用 GLI 框架用 Ruby 编写的命令行实用程序。我想在我的主目录中配置我的命令行实用程序,使用 Ruby 本身作为 DSL 来处理它(类似于 Gemfile 或 Rakefile)。 我
我的 Rails 应用 Controller 中有这段代码: def delete object = model.datamapper_class.first(:sourced_id =>
我正在寻找的解析器应该: 对 Ruby 解析友好, 规则设计优雅, 产生用户友好的解析错误, 用户文档的数量应该比计算器示例多, UPD:允许在编写语法时省略可选的空格。 快速解析不是一个重要的特性。
我刚开始使用 Ruby,听说有一种“Ruby 方式”编码。除了 Ruby on Rails 之外,还有哪些项目适合学习并被认可且设计良好? 最佳答案 Prawn被明确地创建为不仅是一个该死的好 PDF
我知道之前有人问过类似的问题,但是我该如何构建一个无需在前面输入“ruby”就可以在终端中运行的 Ruby 文件呢? 这里的最终目标是创建一个命令行工具包类型的东西。现在,为了执行我希望用户能够执行的
例如哈希a是{:name=>'mike',:age=>27,:gender=>'male'}哈希 b 是 {:name=>'mike'} 我想知道是否有更好的方法来判断 b 哈希是否在 a 哈希内,而
我是一名决定学习 Ruby 和 Ruby on Rails 的 ASP.NET MVC 开发人员。我已经有所了解并在 RoR 上创建了一个网站。在 ASP.NET MVC 上开发,我一直使用三层架构:
最近我看到 Gary Bernhardt 展示了他用来在 vim 中执行 Ruby 代码的 vim 快捷方式。捷径是 :map ,t :w\|:!ruby %. 似乎这个方法总是执行系统 Rub
在为 this question about Blue Ruby 选择的答案中,查克说: All of the current Ruby implementations are compiled to
我有一个 Ruby 数组 > list = Request.find_all_by_artist("Metallica").map(&:song) => ["Nothing else Matters"
我在四舍五入时遇到问题。我有一个 float ,我想将其四舍五入到小数点后的百分之一。但是,我只能使用 .round ,它基本上将它变成一个 int,意思是 2.34.round # => 2. 有没
我使用 ruby on rails 编写了一个小型 Web 应用程序,它的主要目的是上传、存储和显示来自 xml(文件最多几 MB)文件的结果。运行大约 2 个月后,我注意到 mongrel 进程
我们如何用 Ruby 转换像这样的字符串: 𝑙𝑎𝑡𝑜𝑟𝑟𝑒 收件人: Latorre 最佳答案 s = "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" => "𝑙𝑎𝑡𝑜𝑟𝑟𝑒" s.u
通过 ruby monk 时,他们偶尔会从左侧字段中抛出一段语法不熟悉的代码: def compute(xyz) return nil unless xyz xyz.map {|a,
不确定我做错了什么,但我似乎弄错了。 问题是,给你一串空格分隔的数字,你必须返回最大和最小的数字。 注意:所有数字都是有效的 Int32,不需要验证它们。输入字符串中始终至少有一个数字。输出字符串必须
我是一名优秀的程序员,十分优秀!