我有一個CSV與一些文件名和日期:紅寶石:問題解析CSV,並通過行循環
"doc_1.doc", "date1"
"doc_2.doc", "date2"
"doc_5.doc", "date5"
的問題是,有許多空白文件號之間,如:doc_2
和doc_5
我正在嘗試編寫一個腳本來解析CSV,並通過比較每行並填寫必要的空白處填充空白處。
例如在這個例子中,它會增加
"doc_3.doc", "date copied from date2"
"doc_4.doc", "date copied from date2"
我想因爲我想學習的語言寫在Ruby中這個腳本,並明確我誤解的方式Ruby的循環工作,因爲它不是典型的「爲「循環在PHP等
在這裏,人們經常使用的是到目前爲止我的代碼,用循環任何幫助自身將不勝感激!
#!/usr/bin/env ruby
require 'csv'
# Load file
csv_fname = './upload-list-docs.csv'
# Parsing function
def parse_csv(csv_fname)
uploads = []
last_number = 0
# Regex to find number in doc_XXX.YYY
regex_find_number = /(?<=\_)(.*?)(?=\.)/
csv_content = CSV.read(csv_fname)
# Skip header row
csv_content.shift
csv_content.each do |row|
current_number = row[0].match regex_find_number
current_date = row[1]
last_date = current_date
until last_number == current_number do
uploads << [last_number, last_date]
last_number += 1
end
end
return uploads
end
puts parse_csv(csv_fname)
和一些示例CSV
"file_name","date"
"doc_1.jpg","2011-05-11 09:16:05.000000000"
"doc_3.doc","2011-05-11 10:10:36.000000000"
"doc_4.doc","2011-05-11 10:17:19.000000000"
"doc_6.doc","2011-05-11 10:58:35.000000000"
"doc_7.pdf","2011-05-11 11:16:22.000000000"
"doc_8.pdf","2011-05-11 11:19:29.000000000"
"doc_9.docx","2011-05-11 11:40:03.000000000"
"doc_13.pdf","2011-05-11 12:26:32.000000000"
"doc_14.docx","2011-05-11 12:34:50.000000000"
"doc_15.doc","2011-05-11 12:40:12.000000000"
"doc_16.doc","2011-05-11 13:03:11.000000000"
"doc_17.doc","2011-05-11 13:03:58.000000000"
"doc_19.pdf","2011-05-11 13:25:07.000000000"
"doc_20.rtf","2011-05-11 13:34:26.000000000"
"doc_21.rtf","2011-05-11 13:35:25.000000000"
"doc_24.doc","2011-05-11 13:49:02.000000000"
"doc_25.doc","2011-05-11 14:05:04.000000000"
"doc_26.pdf","2011-05-11 14:18:26.000000000"
"doc_27.rtf","2011-05-11 14:30:19.000000000"
"doc_28.doc","2011-05-11 14:33:13.000000000"
"doc_29.jpg","2011-05-11 15:07:27.000000000"
"doc_30.doc","2011-05-11 15:22:30.000000000"
"doc_31.doc","2011-05-11 15:31:07.000000000"
"doc_34.doc","2011-05-11 15:51:56.000000000"
"doc_35.doc","2011-05-11 15:55:15.000000000"
"doc_36.doc","2011-05-11 16:06:46.000000000"
"doc_38.wps","2011-05-11 16:21:08.000000000"
"doc_39.doc","2011-05-11 16:30:57.000000000"
"doc_40.doc","2011-05-11 16:41:55.000000000"
"doc_43.JPG","2011-05-11 17:03:40.000000000"
"doc_46.doc","2011-05-11 17:28:13.000000000"
"doc_51.doc","2011-05-11 17:50:34.000000000"
"doc_52.doc","2011-05-11 18:03:13.000000000"
"doc_53.doc","2011-05-11 18:43:48.000000000"
"doc_54.doc","2011-05-11 18:54:45.000000000"
"doc_55.doc","2011-05-11 19:31:03.000000000"
"doc_56.doc","2011-05-11 19:31:23.000000000"
"doc_57.doc","2011-05-11 20:17:38.000000000"
"doc_59.jpg","2011-05-11 20:22:55.000000000"
"doc_61.pdf","2011-05-11 21:14:52.000000000"
當你運行這段代碼時會發生什麼? –
你會得到一個無限循環,對吧? –
是的,無限循環,因爲'current_number'永遠不會改變。 – waffl