Sorry for the newbie question but is it possible to delete a data entry,
im only able to delete the whole table. But i’d like to do something
like:
csv before:
ID|someData|alsoData|
1,hello,world
2,also,helloWolrd
3,fill,stuff
csv after:
1,hello,world
3,fill,stuff
And delete the entry by the ID 2. is this possible with csv and ruby?
Sorry for my shitty english.
dev1
2
See the docs:
For example:
require 'csv'
csv_test = "Id, Name, Location
1,NameA,LocA
2,NameB,LocB
3,NameC,LocC"
csv = CSV.new(csv_test, :headers => :first_row, :return_headers =>
true).read
csv.by_row!
csv.delete_if do |row|
!row.header_row? && row.field(“Id”) == “2”
end
CSV.open("csv_out.csv","wb") do |csv_out|
csv.by_row!
csv.each{ |row| csv_out << row }
end
$ cat csv_out.csv
Id, Name, Location
1,NameA,LocA
3,NameC,LocC
dev1
3
Dansei Yuuki wrote in post #1177459:
See the docs:
For example:
require 'csv'
csv_test = "Id, Name, Location
1,NameA,LocA
2,NameB,LocB
3,NameC,LocC"
csv = CSV.new(csv_test, :headers => :first_row, :return_headers =>
true).read
csv.by_row!
csv.delete_if do |row|
!row.header_row? && row.field(“Id”) == “2”
end
CSV.open("csv_out.csv","wb") do |csv_out|
csv.by_row!
csv.each{ |row| csv_out << row }
end
$ cat csv_out.csv
Id, Name, Location
1,NameA,LocA
3,NameC,LocC
thank you so much, for the fast reply.
ill try it soon as possible
dev1
4
i’ve tried it and, it works perfect.
thank you blutorange