Ruby: How can I edit the contents of a CSV file? - ruby

As a follow up to this question, how can I edit the contents of a CSV file?
I ultimately need to replace all instances of something and I need to do that before I open the file for parsing (with FasterCSV).
So, using Ruby I need to open, edit, and save the CSV file.

you can use the CSV module.

Related

Parsing a JSON file without JSON.parse()

This is my first time using Ruby. I'm writing an application that parses data and performs some calculations based on it, the source of which is a JSON file. I'm aware I can use JSON.parse() here but I'm trying to write my program so that it will work with other sources of data. Is there a clear cut way of doing this? Thank you.
When your source file is JSON then use JSON.parse. Do not implement a JSON parser on your own. If the source file is a CSV, then use the CSV class.
When your application should be able to read multiple different formats then just add one Reader class for each data type, like JSONReader, CSVReader, etc. And then decide depending on the file extension which reader to use to read the file.

Getting inverted commas appended in request while reading from csv in Jmeter?

I was trying to read csv file using csvdata config element in jmeter so as to test multiple logins but when I try to read the value from csv file then I get inverted commas appended with respect to result. Please tell me how to get rid of these commas being passed in the request parameters
Please find my csv data config and excel file and request parameter screenshot in attachments
JMeter normally doesn't add anything to the variables, most probably you have the quotation marks in the generated CSV file, open it with normal text editor like Notepad and use find-and-replace feature to remove the quotation marks from there.
If you cannot efficiently control the CSV data you can use __strReplace() function in order to remove the quotation marks from the variables originating from the CSV Data Set Config on the fly like:
${__strReplace(${Username},\",,)}
Demo:
You can install __strReplace() function as well as other Custom JMeter Functions using JMeter Plugins Manager
I had the same issue when I opened a csv file as a normal text file, I saw a Values, value2.
After removing it, it started working as expected.

ruby excel - write data to existing xls

I have tried different gems in ruby and also searched a lot but Ruby doesnt seem to have a solution to write to existing excel file.
my excel file 'services.xls' has 3 columns
1st column name is 'inputxlm'
2nd column name is 'methodtoexecute'
3rd column name is 'output'
i have internal logic which takes the inputxlm, process it using the method and generates output
How do i write back the output to output column in 'services.xls' ?
Note : I dont want to use win32ole as my organization has some limitation on it
This article is a great source to find out which library suits you best: https://spin.atomicobject.com/2017/03/22/parsing-excel-files-ruby/
My company uses axlsx combined with axlsx_rails to render xls files with Rails rendering machine and axlsx_styler for styling.
Note that in a simple use case like the one you describe, you night not need an excel file like xls, a mere CSV would suffice, and for that, Ruby has CSV

How to add content to a worksheet by referencing its name in ruby using axlsx gem?

I have created a workbook with multiple worksheets using axlsx. I've also given names to each of the worksheets. Now, I want to add content to one of the existing worksheets. Is there any way to reference the worksheet I want to add content to? That would be the easiest way.
I've also read that somewhere that you could use another gem to read the excel file then modify it. Does it have to be one with read/write capabilities like rubyXL or can I use one gem to read and then use axlsx again to write on it?

How to simply pull information from excel to use as a variable ruby

I am trying to pull product ID's, that are in Excel, into ruby to use as a temporary variable. I cannot find a simple solution and am not sure if I should use the spreadsheet gem or axlsx gem. If you guys could help get me started that would be amazing!
One simple solution would be to save the excel document as a csv. Then it would be relatively easy to to open the file with your ruby program, read in the data you need and put that into a variable, for example using the CSV library
CSV.foreach("path/to/file.csv") do |row|
# use row here
end

Resources