简体   繁体   English

上载的文件中有空行Ruby

[英]Uploaded file has empty rows Ruby

When user browses files and clicks submit, I have to upload file to public folder location of rails application. 当用户浏览文件并单击提交时,我必须将文件上传到Rails应用程序的公用文件夹位置。 I'm successful to upload this. 我上传成功了。

But uploaded file has empty rows. 但是上传的文件有空行。 How to upload file without empty rows? 如何上传没有空行的文件?

Original File 原始文件

1.ID      Division     Organisation
2.EA011  Maintenance   Repairs
3.EA012  Application   Production

I have to validate headers across rows and then update UI for every row processed. 我必须验证跨行的标题,然后为处理的每一行更新UI。 Say i have to validate ID and then show in a form below submit button in same page where i uploaded file. 说我必须验证ID,然后在我上传文件的同一页面中的“提交”按钮下方的表单中显示。 How can i update form after processing each row. 处理每一行后如何更新表格。 and print ID EA011 is valid. 并且打印ID EA011有效。

If a file has empty rows, then it has empty rows--you can't change that. 如果文件有空行,则它有空行-您无法更改它。 However, you can always remove the empty rows from a file: 但是,您始终可以从文件中删除空行:

#Here's a file with an empty row:
File.open('stuff.txt', 'w') do |f|
  f.puts("hello\n\nworld")
end

#Remove the empty rows:
File.open('stuff2.txt', 'w') do |fout|
  IO.foreach('stuff.txt') do |line|
    next if line =~ /\A\s+\z/
    fout.write(line)
  end
end

--output:--
$ ruby myprog.rb
$ cat stuff.txt
hello

world
~/ruby_programs$ cat stuff2.txt
hello
world

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM