Say I have an array of some elements, and I want to save the data related to each element in a CSV file. This is my code:
require 'rubygems'
require 'csv'
category_ID = [-1, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 15, 17, 18, 19, 20, 21, 91, 92, 93, 94, 101, 102, 103, 104, 105, 106, 107, 111, 112, 113, 114, 115, 116, 117, 118, 119]
CSV.foreach('C:\Users\username\Desktop\Original.csv', :headers=>true) do |row,i|
category_ID.each {|x|}
CSV.open('C:\Users\username\Desktop\#{x}.csv', "wb") do |csv|
if row[4].gsub(/^[^:]+:\s*/,"") == x
csv << row[28].gsub(/^[^:]+:\s*/,"")
end
end
end
end
So I want my file names to be -1.csv, 0.csv, 1.csv, ... , 119.csv. But it is giving an error because of the #{x} as the file name. How to correct this? Thanks.
EDIT: This is 1 row from the Original.csv file:
"{""timestamp"""," ""2015-05-10 15",59,"59"""," ""article_category_id"""," ""106"""," ""app_name"""," ""Apple Daily TW"""," ""ip"""," ""111.80.37.218"""," ""app_id"""," ""51ee0d904055aa1539d32523"""," ""datetime"""," ""1431273612198"""," ""edition"""," """""," ""action"""," ""page_view"""," ""article_title"""," ""【溫星母親節】潘若迪竟讓老婆一個人過"""," ""user_id"""," """""," ""menu"""," ""即時新聞"""," ""lon"""," """""," ""platform"""," ""Android"""," ""country_residence"""," """""," ""version"""," ""2.5.2"""," ""page_name"""," ""【溫星母親節】潘若迪竟讓老婆一個人過"""," ""location"""," """""," ""is_registered"""," ""False"""," ""provider"""," """""," ""page_id"""," ""607814"""," ""menu_id"""," ""10002"""," ""article_category"""," ""娛樂"""," ""lat"""," """""," ""article_id"""," ""607814"""," ""country_origin"""," """""," ""udid"""," ""d0155049772de9"""," ""gender"""," """""," ""age"""," """""," ""device"""," ""C6902"""," ""session_id"""," """""," ""push"""," ""N"""," ""os"""," ""4.4.2"""," ""browser"""," """"}",,
usernamepart.