簡體   English   中英

上載的文件中有空行Ruby

[英]Uploaded file has empty rows Ruby

當用戶瀏覽文件並單擊提交時,我必須將文件上傳到Rails應用程序的公用文件夾位置。 我上傳成功了。

但是上傳的文件有空行。 如何上傳沒有空行的文件?

原始文件

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

我必須驗證跨行的標題,然后為處理的每一行更新UI。 說我必須驗證ID,然后在我上傳文件的同一頁面中的“提交”按鈕下方的表單中顯示。 處理每一行后如何更新表格。 並且打印ID EA011有效。

如果文件有空行,則它有空行-您無法更改它。 但是,您始終可以從文件中刪除空行:

#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