当前位置: 首页 > 工具软件 > FasterCSV > 使用案例 >

fasterCSV操作

拓拔烨赫
2023-12-01
[color=red]fasterCSV [/color]
This class provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.
[color=red][b]读[/b] [/color]
1.从一个文件读 每次读取一行
FasterCSV.foreach("path/to/file.csv") do |row| # use row here... end

全部读取
arr_of_arrs = FasterCSV.read("path/to/file.csv") 

2.从字符串里读
A Line at a Time
FasterCSV.parse("CSV,data,String") do |row| 
# use row here... end

All at Once
arr_of_arrs = FasterCSV.parse("CSV,data,String") 

[color=red][b]写[/b][/color]
To a File
FasterCSV.open("path/to/file.csv", "w") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ... end

To a String

csv_string = FasterCSV.generate do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ... end

Convert a Single Line
csv_string = ["CSV", "data"].to_csv # to CSV 
csv_array = "CSV,String".parse_csv # from CSV
Shortcut Interface
  
FCSV { |csv_out| csv_out << %w{my data here} } # to $stdout
FCSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String
FCSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr

原文:[url]http://fastercsv.rubyforge.org/[/url]
 类似资料: