ruby处理文本
来源:互联网 发布:ios游戏数据修改方法 编辑:程序博客网 时间:2024/05/21 23:02
# 详情请查看../ruby/lib/ruby/1.8目录下的fileutils.rb文件
一 目录的部分操作
1. 新建文件夹
Dir.mkdir("f:/testdir")
2. 删除文件夹
Dir.rmdir("f:/testdir")
3. 查询当前目录下的文件
puts Dir.entries(File.join("f:","Ruby"))
二 文件的部分操作
在开始之前,先介绍一下ruby对文件处理的各种模式:
"r" :Read-only. Starts at beginning of file (default mode).
翻译:"r" 只读。从文件起始处开始读取(默认模式)
"r+" :Read-write. Starts at beginning of file.
翻译:"r+"可读可写,从文件的起始处开始读取
"w" :Write-only. Truncates existing file to zero length or creates a new file for writing.
翻译:"w"只写,截断现有文件的长度至零,或建立一个新文件,以备写入
"w+" :Read-write. Truncates existing file to zero length or creates a new file for reading and writing.
翻译:"w+"可读可写,截断现有文件的长度至零,或建立一个新文件,以备读写
"a" :Write-only. Starts at end of file if file exists; otherwise, creates a new file for writing.
翻译:"a"只写,若文件存在,在由文件结尾处开始写入,或则建立新文件以备写入
"a+" :Read-write. Starts at end of file if file exists; otherwise, creates a new file for reading and writing.
翻译:"a+"可读可写,若文件存在,在由文件结尾处开始写入,或则建立新文件以备写入
"b" :(DOS/Windows only.) Binary file mode. May appear with any of the key letters listed above。
翻译:"b" (Dos/Windows限定功能)二进制文件模式,可与上述各关键字一并出现
1. 新建文件
f=File.new(File.join("f:/ruby","Test.xml"), "w+") # 文件名和格式任意
2. 往文件中输入内容
f.puts("<?xml version='1.0' encoding='utf-8'?>")
f.puts('<Person>')
f.puts("<Name>#{NAME}</Name>")
f.puts("<Age>55</Age>")
f.puts("<Weight>120KG</Weight>")
f.puts('</Person>') # 因为在捣鼓xml配置文件,就临时复制过来了,内容随意
3. 删除文件
path_file = File.join("f:/ruby","Test.xml")
object_file=File.new(path_file, "w+")
object_file.close # 缺少这一步,会报“in `delete': Permission denied - f:/ruby/Test.txt (Errno::EACCES)”
File.delete(path_file)
1.ruby 对word文档的处理
#~ #1.统计word文档的图片数、表格数、页数、字数;
#~ #2.按照段落抽取word文档中的内容;
$KCODE = "e"
require "win32ole"
word = WIN32OLE.new("word.Application")
word.visible = false
doc = word.Documents.open("D:/w.docx")
doc.Activate
#~ p doc.Content.Text.split(//).size
#~ doc.CopyStylesFromTemplate("D:/r.docx")
#~ p doc.Content.Pages
p doc.ComputeStatistics(0)#字数
p doc.ComputeStatistics(1)#行数
p doc.ComputeStatistics(2)#页数
p doc.ComputeStatistics(3)#字符数(不计空格)
p doc.ComputeStatistics(4)#段落数
p doc.ComputeStatistics(5)#字符数(计空格)
p doc.ComputeStatistics(6)#中文字符和朝鲜语字符
p doc.InlineShapes.Count #图片数
p doc.Tables.Count #表格数
p doc.Paragraphs.Count#段数
p i = doc.Paragraphs.count#段数
for n in 1..i
str=doc.Paragraphs(n).Range.text#按段数提取内容
st=str.split(/[\r\a\s]/).join("")
p st if st!=""
end
2.ruby对txt文档的处理
0 0
- ruby处理文本
- ruby html转纯文本
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- 文本处理
- ruby的异常处理
- Ruby 异常处理
- [Ruby]字符串处理
- 剑指Offer:面试题42 反转单词顺序
- [UnitTest04]-Moq判断方法的执行情况
- qsort的使用以及*(char**)p与(char*)p之间的区别
- C++中数字与字符串之间的转换
- stm32学习笔记——按键(外部中断)
- ruby处理文本
- 剑指Offer:面试题43 n个骰子的点数
- Oracle -PLSQL存储过程游标当出参
- ruby加载路径$Load_Path
- 剑指Offer:面试题44 扑克牌的顺子
- hdu 4003 Find Metal Mineral(树形DP+分组背包)
- 测试博客中插入LATEX公式
- cocos2d-x 读取文件
- Ros.Ardrone(tum_ardrone) 的编译与运行