我似乎无法让这个工作.我想从不同的Web服务器中提取CSV文件以读取我的应用程序.这就是我想要的方式:
url = 'http://www.testing.com/test.csv'
records = FasterCSV.read(url, :headers => true, :header_converters => :symbol)
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我试过谷歌搜索,我想出的就是这个摘录:实用的红宝石宝石
所以,我尝试修改如下:
require 'open-uri'
url = 'http://www.testing.com/test.csv'
csv_url = open(url)
records = FasterCSV.read(csv_url, :headers => true, :header_converters => :symbol)
Run Code Online (Sandbox Code Playgroud)
...我得到一个can't convert Tempfile into String错误(来自FasterCSV宝石).
谁能告诉我如何使这项工作?
我无法理解FasterCSV中的:header_converters和:转换器.基本上,我想要做的就是将列标题更改为适当的列名.
就像是:
FasterCSV.foreach(csv_file, {:headers => true, :return_headers => false, :header_converters => :symbol, :converters => :all} ) do |row|
puts row[:some_column_header] # Would be "Some Column Header" in the csv file.
Run Code Online (Sandbox Code Playgroud)
execpt我不理解:符号和:全部在转换器参数中.
嘿.如何获取文件中的总行数(不想使用循环).我正在读CSV文件.
例1
CSV.open('clients.csv', 'r')
Run Code Online (Sandbox Code Playgroud)
例2
FasterCSV.foreach('clients.csv')
Run Code Online (Sandbox Code Playgroud)
谢谢.
我一直在将CSV文件上传到Heroku并进行处理时遇到问题.它在我的本地环境中工作正常.请注意,我不需要在Heroku上保存文件,只需在请求期间访问它,以便将其转换为字符串以进行处理并导入到DB中.
我想做的是:
控制器代码:
def create
@account = Account.find(params[:report][:account_id])
@file = params[:report][:file].read
# logger.info file.inspect
case @account.provider
when "Microsoft AdCenter" then @file.gsub!(/\A(.*)\n\n/im, "")
when "Google AdWords" then @file.gsub!(/\A(.*)\n/i, "")
else
raise "Invalid PPC report format"
end
end
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪:
Processing ImportController#create (for XX.182.6.XXX at 2010-09-11 09:19:01) [POST]
Parameters: {"commit"=>"Upload", "action"=>"create", "authenticity_token"=>"XXXXXwoFpvRO3vN8XVXRDg8rikFsj2TFTW7mrcTgg=", "controller"=>"import", "report"=>{"account_id"=>"1", "file"=>#<File:/home/slugs/126077_0657264_9a92/mnt/tmp/RackMultipart.9845.0>}}
NoMethodError (private method `gsub!' called for #<Tempfile:0x2b8ccb63ece0>):
/usr/local/lib/ruby/1.8/delegate.rb:270:in `method_missing'
app/controllers/import_controller.rb:15:in `create'
warden (0.10.7) lib/warden/manager.rb:35:in `call'
warden (0.10.7) lib/warden/manager.rb:34:in `catch'
warden (0.10.7) lib/warden/manager.rb:34:in `call'
/home/heroku_rack/lib/static_assets.rb:9:in `call' …Run Code Online (Sandbox Code Playgroud) 我正在使用FasterCSV在Rails 3应用程序中生成报告的CSV输出.这是一段代码片段:
<%= FasterCSV.generate do |csv|
@groups.each do |b|
record = [ b.group, b.organization_name, b.status, b.comments ]
csv << record
end
end
%>
Run Code Online (Sandbox Code Playgroud)
当FasterCSV包含空字符串时,它使用一对空的双引号.
不幸的是,Rails 3将这些引号编码为实体,这对Excel不起作用.这是我的CSV输出的样子(当b.comments为nil或空字符串时):
Rafeland,Rafe Organization,Submitted,""
Run Code Online (Sandbox Code Playgroud)
什么是阻止Rails编码这些实体的普遍接受的方法?我知道"原始"方法,但它不会阻止我可以将CSV生成.
FasterCSV在此行中引发了MalformedCSVError(非法引用):
|0150|1161623|Medicamentos e genericos "EPP".|1423|PB|
Run Code Online (Sandbox Code Playgroud)
这是代码:
FasterCSV.foreach(path_to_file, :col_sep => '|') do |row|
...
end
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?韩国社交协会!
我正在尝试使用CSV作为SiriProxy项目插件中的设置文件来使用wake-on-lan.这个项目基于ruby.
所以csv如下:
Name, MACAddress
Desktop, 01-23-45-67-89-ab
Computer, 02-46-81-02-46-cd
Run Code Online (Sandbox Code Playgroud)
等等...
所以我想要发生的是,例如当变量userAction是"桌面"时,我查询CSV并将MAC地址返回到另一个变量.我迷失了如何做到这一点.我见过csv和faster_csv,但不知道如何让这些工作像这样.
提前致谢!
我正在尝试将大量数据从数据库导出到csv文件,但这需要很长时间,并且担心我会遇到重大内存问题.
有没有人知道在没有内存累积的情况下导出CSV的更好方法?如果是的话,你能告诉我怎么样吗?谢谢.
这是我的控制器:
def users_export
File.new("users_export.csv", "w") # creates new file to write to
@todays_date = Time.now.strftime("%m-%d-%Y")
@outfile = @todays_date + ".csv"
@users = User.select('id, login, email, last_login, created_at, updated_at')
FasterCSV.open("users_export.csv", "w+") do |csv|
csv << [ @todays_date ]
csv << [ "id","login","email","last_login", "created_at", "updated_at" ]
@users.find_each do |u|
csv << [ u.id, u.login, u.email, u.last_login, u.created_at, u.updated_at ]
end
end
send_file "users_export.csv",
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{@outfile}"
end
Run Code Online (Sandbox Code Playgroud) 我写了这个rake任务,使我能够从我的应用程序的本地文件系统上的文件中读取csv文件,但是如何调整它以使其从URL读取文件?
desc "This class will read a csv file and display its contents on the screen"
task :read_csv => :environment do |t, args|
require "csv"
csv_text = File.read('someFile.csv')
csv = CSV.parse(csv_text, :headers=>true)
csv.each do |row|
puts row
end
end
Run Code Online (Sandbox Code Playgroud)
如果有人可以帮助我使用代码或一些当前链接,将不胜感激.我发现的大多数链接都是以前版本的rails,其中FasterCSV不是ruby的一部分.
谢谢
我正在努力抓住如何有效地使用FasterCSV来实现我想要的目标.
我有一个CSV文件; 说:
ID,day,site
test,tuesday,cnn.com
bozo,friday,fark.com
god,monday,xkcd.com
test,saturday,whatever.com
Run Code Online (Sandbox Code Playgroud)
我要通过这个文件,最后得到一个散列,其中包含第一列发生次数的计数器.所以:
["test" => 2, "bozo" => 1, "god" => 1]
Run Code Online (Sandbox Code Playgroud)
我需要能够在不事先了解第一列中的值的情况下执行此操作.
?