How to remove the delimiters from a csv file in ruby - ruby

I am reading a file which is either separated by a "tab space" or "semicolon(;)" or "comma(,)" below code separates only tab space but i want all 3 to be checked. like if a file is comma separated it should work for that also . Please help!
#!/usr/bin/ruby
require 'csv'
test = CSV.read('test.csv', headers:true, :col_sep => "\t")
x = test.headers
puts x

It looks like :col_sep cannot be a Regexp, which could have solved your problem.
One possible solution would be to analyze the head of your CSV file and count the occurences of possible separators :
require 'csv'
possible_separators = ["\t", ';', ',']
lines_to_analyze = 1
File.open('test.csv') do |csv|
head = csv.first(lines_to_analyze).join
#col_sep = possible_separators.max_by { |sep| head.count(sep) }
end
#col_sep
#=> ";"
You can then use :
test = CSV.read('test.csv', headers:true, :col_sep => #col_sep)
x = test.headers
puts x
# a
# b
# c
The values in x won't contain any separator.

Related

How to read multiple XML files then output to multiple CSV files with the same XML filenames

I am trying to parse multiple XML files then output them into CSV files to list out the proper rows and columns.
I was able to do so by processing one file at a time by defining the filename, and specifically output them into a defined output file name:
File.open('H:/output/xmloutput.csv','w')
I would like to write into multiple files and make their name the same as the XML filenames without hard coding it. I tried doing it multiple ways but have had no luck so far.
Sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<record:root>
<record:Dataload_Request>
<record:name>Bob Chuck</record:name>
<record:Address_Data>
<record:Street_Address>123 Main St</record:Street_Address>
<record:Postal_Code>12345</record:Postal_Code>
</record:Address_Data>
<record:Age>45</record:Age>
</record:Dataload_Request>
</record:root>
Here is what I've tried:
require 'nokogiri'
require 'set'
files = ''
input_folder = "H:/input"
output_folder = "H:/output"
if input_folder[input_folder.length-1,1] == '/'
input_folder = input_folder[0,input_folder.length-1]
end
if output_folder[output_folder.length-1,1] != '/'
output_folder = output_folder + '/'
end
files = Dir[input_folder + '/*.xml'].sort_by{ |f| File.mtime(f)}
file = File.read(input_folder + '/' + files)
doc = Nokogiri::XML(file)
record = {} # hashes
keys = Set.new
records = [] # array
csv = ""
doc.traverse do |node|
value = node.text.gsub(/\n +/, '')
if node.name != "text" # skip these nodes: if class isnt text then skip
if value.length > 0 # skip empty nodes
key = node.name.gsub(/wd:/,'').to_sym
if key == :Dataload_Request && !record.empty?
records << record
record = {}
elsif key[/^root$|^document$/]
# neglect these keys
else
key = node.name.gsub(/wd:/,'').to_sym
# in case our value is html instead of text
record[key] = Nokogiri::HTML.parse(value).text
# add to our key set only if not already in the set
keys << key
end
end
end
end
# build our csv
File.open('H:/output/.*csv', 'w') do |file|
file.puts %Q{"#{keys.to_a.join('","')}"}
records.each do |record|
keys.each do |key|
file.write %Q{"#{record[key]}",}
end
file.write "\n"
end
print ''
print 'output files ready!'
print ''
end
I have been getting 'read memory': no implicit conversion of Array into String (TypeError) and other errors.
Here's a quick peer-review of your code, something like you'd get in a corporate environment...
Instead of writing:
input_folder = "H:/input"
input_folder[input_folder.length-1,1] == '/' # => false
Consider doing it using the -1 offset from the end of the string to access the character:
input_folder[-1] # => "t"
That simplifies your logic making it more readable because it's lacking unnecessary visual noise:
input_folder[-1] == '/' # => false
See [] and []= in the String documentation.
This looks like a bug to me:
files = Dir[input_folder + '/*.xml'].sort_by{ |f| File.mtime(f)}
file = File.read(input_folder + '/' + files)
files is an array of filenames. input_folder + '/' + files is appending an array to a string:
foo = ['1', '2'] # => ["1", "2"]
'/parent/' + foo # =>
# ~> -:9:in `+': no implicit conversion of Array into String (TypeError)
# ~> from -:9:in `<main>'
How you want to deal with that is left as an exercise for the programmer.
doc.traverse do |node|
is icky because it sidesteps the power of Nokogiri being able to search for a particular tag using accessors. Very rarely do we need to iterate over a document tag by tag, usually only when we're peeking at its structure and layout. traverse is slower so use it as a very last resort.
length is nice but isn't needed when checking whether a string has content:
value = 'foo'
value.length > 0 # => true
value > '' # => true
value = ''
value.length > 0 # => false
value > '' # => false
Programmers coming from Java like to use the accessors but I like being lazy, probably because of my C and Perl backgrounds.
Be careful with sub and gsub as they don't do what you're thinking they do. Both expect a regular expression, but will take a string which they do a escape on before beginning their scan.
You're passing in a regular expression, which is OK in this case, but it could cause unexpected problems if you don't remember all the rules for pattern matching and that gsub scans until the end of the string:
foo = 'wd:barwd:' # => "wd:barwd:"
key = foo.gsub(/wd:/,'') # => "bar"
In general I recommend people think a couple times before using regular expressions. I've seen some gaping holes opened up in logic written by fairly advanced programmers because they didn't know what the engine was going to do. They're wonderfully powerful, but need to be used surgically, not as a universal solution.
The same thing happens with a string, because gsub doesn't know when to quit:
key = foo.gsub('wd:','') # => "bar"
So, if you're looking to change just the first instance use sub:
key = foo.sub('wd:','') # => "barwd:"
I'd do it a little differently though.
foo = 'wd:bar'
I can check to see what the first three characters are:
foo[0,3] # => "wd:"
Or I can replace them with something else using string indexing:
foo[0,3] = ''
foo # => "bar"
There's more but I think that's enough for now.
You should use Ruby's CSV class. Also, you don't need to do any string matching or regex stuff. Use Nokogiri to target elements. If you know the node names in the XML will be consistent it should be pretty simple. I'm not exactly sure if this is the output you want, but this should get you in the right direction:
require 'nokogiri'
require 'csv'
def xml_to_csv(filename)
xml_str = File.read(filename)
xml_str.gsub!('record:','') # remove the record: namespace
doc = Nokogiri::XML xml_str
csv_filename = filename.gsub('.xml', '.csv')
CSV.open(csv_filename, 'wb' ) do |row|
row << ['name', 'street_address', 'postal_code', 'age']
row << [
doc.xpath('//name').text,
doc.xpath('//Street_Address').text,
doc.xpath('//Postal_Code').text,
doc.xpath('//Age').text,
]
end
end
# iterate over all xml files
Dir.glob('*.xml').each { |filename| xml_to_csv(filename) }

How can i read lines in a textfile with RUBY

I am new in ruby programming. I am trying to read a textfile line by line.
Here is my sample textfile:
john
doe
john_d
somepassword
Here is my code:
f = File.open('input.txt', 'r')
a = f.readlines
n = a[0]
s = a[1]
u = a[2]
p = a[3]
str = "<user><name=\"#{n}\" surname=\"#{s}\" username=\"#{u}\" password=\"#{p}\"/></user>"
File.open('result.txt', 'w') { |file| file.print(str) }
The output should look like this:
<user><name="john" surname="doe" username="john_d" password="somepassword"/></user>
But the result.txt looks like this. It includes newline character for every line:
<user><name="john
" surname="doe
" username="john_d
" password="somepassword"/></user>
How can i correct this?
It includes newline character for every line, because there is a newline character at the end of every line.
Just removed it when you don't need it:
n = a[0].gsub("\n", '')
s = a[1].gsub("\n", '')
# ...
As explained by spickermann, also just change line two into:
a = f.readlines.map! { |line| line.chomp }
As #iGian already mentioned, chomp is a good option to clean up your text. I am not sure which version of Ruby you are using, but here is the link to the official Ruby version 2.5 documentation on chomp just so you see how it is going to help you: https://ruby-doc.org/core-2.5.0/String.html#method-i-chomp
See the content of variable a after using chomp:
2.4.1 :001 > f = File.open('input.txt', 'r')
=> #<File:input.txt>
2.4.1 :002 > a = f.readlines.map! {|line| line.chomp}
=> ["john", "doe", "john_d", "somepassword"]
Depending on how many other corner cases you expect to see from your input string, here is also another suggestion that can help you to clean up your strings: strip with link to its official documentation with examples: https://ruby-doc.org/core-2.5.0/String.html#method-i-strip
See the content of variable a after using strip:
2.4.1 :001 > f = File.open('input.txt', 'r')
=> #<File:input.txt>
2.4.1 :002 > a = f.readlines.map! {|line| line.strip}
=> ["john", "doe", "john_d", "somepassword"]
FName = 'temp'
File.write FName, "john
doe
john_d
somepassword"
#=> 28
Here are two ways.
s = "<user><name=\"%s\" surname=\"%s\" username=\"%s\" password=\"%s\"/></user>"
puts s % File.readlines(FName).map(&:chomp)
# <user><name="john" surname="doe" username="john_d" password="somepassword"/></user>
puts s % File.read(FName).split("\n")
# <user><name="john" surname="doe" username="john_d" password="somepassword"/></user>
See String#% and, as mentioned in that doc, Kernel#sprintf.

Filter unique values from a tsv file

I have a tsv file that has four columns. I'm having difficulty isolating the first column of the file (UUID), so I can strip out the 'UUID=' from each element, and also filter from unique values.
What am I doing wrong in my code? I've been pretty stuck on figuring this out. Thank you in advance!
Here's the link to the file, and my code below.
https://drive.google.com/file/d/1mGaK3n3YCrzrwOgSo5QQZ62FXDKJ3nZ8/view?usp=sharing
require "csv"
log_file = CSV.foreach("output_file.tsv",{:col_sep => "\t", :headers => true}) do |row|
uuid = row["UUID"]
ip = row["IP"]
time = row["TIME"]
ua = row["UA"]
uuid = uuid.drop(1)
ip = ip.drop(1)
time = time.drop(1)
ua = ua.drop(1)
uuid = uuid.map { |element|
element = element[5..-1]}
unique_logins = uuid.uniq
puts uuid.uniq.length
Probably you're confused a bit and think that CSV.foreach reads the whole column, but it actually reads your file row by row. That's why no need to drop(1).
This is the minimal code, which collects uuids from the file and prints the number of those uuids and then prints the number of unique uuids
require "csv"
uuids = []
log_file = CSV.foreach("output_file.tsv",{:col_sep => "\t", :headers => true}) do |row|
uuids << row["UUID"]
end
uuids = uuids.map { |element| element = element[5..-1]}
p uuids.length
unique_logins = uuids.uniq
p unique_logins.length
If your file isn't that big, you could also just read the entire file in at once, and then use the returned CSV::Table to read the entire column out and operate on that:
require 'csv'
tsv = CSV.read("output_file.tsv", col_sep: "\t", headers: true)
uuids = tsv['UUID'].map { |uuid| uuid[/\AUUID=(.+)\z/, 1] }.uniq
# => ["e9fc3b6e6641e69fb8cfbdfac48709ae", "f296020354e8c913454f62732d0e3dc4",
# "0300481b1e495e3c919b5214dda7b26c", "9ccc4096ed1d11d1b4c9e57ca1192176",
# "c0580eeb3f98d9c3fe232fc48694bf8e", "25ee63a754b9d4590b69b9ab2a4668cd",
# "aa61387f01797a839ca6f55daeb69b30", "9c7f37f5c187f662eaf7d0df83ac8804"]

Change Headers for Certain Columns in CSV File

I have a CSV file that I want to change the headers only for certain columns (about 20 of them in my actual file). Here's a sample CSV file:
CSV File
"name","blah_01_blah","foo_1_01_foo","bacon_01_bacon","bacon_02_bacon"
"John","yucky","summer","yum","food"
"Mary","","","cool","sundae"
I have been trying this with a File/IO class, but when it reads the file to do the gsub it removes all of the quotation marks around each string separated by commas. Here's the code I'm using:
Ruby Code
file = 'file.csv'
replacements = {
'blah_01_blah' => 'newblah1',
'foo_01_foo' => 'coolfoo1',
'bacon_01_bacon' => 'goodpig1',
'bacon_01_bacon' => 'goodpig2'
}
matcher = /#{replacements.keys.join('|')}/
outdata = File.read(file).gsub(matcher, replacements)
File.open(file, 'w') do |out|
out << outdata
end
What I end up with is this in the CSV file:
New CSV File
name,blah_01_blah,foo_1_01_foo,bacon_01_bacon,bacon_02_bacon
John,yucky,summer,yum,food
Mary,"","",cool,sundae
It's keeping the quotation marks in fields that are blank, but taking them out around the strings elsewhere. I want to retain those quotation marks in case for some reason a rogue comma ends up in a string somewhere so it doesn't get thrown off. How can I change the headers without losing my quotation marks around the strings?
EDIT - This is what I want the file to look like at the end.
Expected Result CSV File
"name","newblah1","coolfoo1","goodpig1","goodpig2"
"John","yucky","summer","yum","food"
"Mary","","","cool","sundae"
Thanks!
You don’t need to handle CSV at all:
File.write(
file,
File.readlines(file).tap do |lines|
lines.first.gsub!(matcher, replacements)
end.join
)
File#readlines.
The trick here is we actually deal with the first line only, as with plain text.
Let's first create the input CSV file.
text =<<_
"name","blah_01_blah","foo_1_01_foo","bacon_01_bacon","bacon_02_bacon"
"John","yucky","summer","yum","food"
"Mary","","","cool","sundae"
_
file_in = 'file_in.csv'
file_out = 'file_out.csv'
File.write(file_in, text)
#=> 137
Here is the replacements hash, which I simplified slightly.
replacements = {'blah_01_blah'=>'newblah1', 'foo_01_foo'=>'coolfoo1',
'bacon_01_bacon'=>'goodpig1'}
The first task is to modify this hash so that if it has no key k, replacements[k] will return k. For this we use the method Hash#default_proc=.
replacements.default_proc = ->(_,k) { k }
Here are two examples of how this hash is used.
replacements['bacon_01_bacon']
#=> "goodpig1"
replacements['name']
#=> "name"`
The latter follows because replacements has no key 'name'.
The code is as follows.
require 'csv'
f_in = CSV.read(file_in, headers:true)
CSV.open(file_out, 'w') do |csv_out|
csv_out << replacements.values_at(*f_in.headers)
f_in.each { |row| csv_out << row }
end
#=> #<CSV::Table mode:col_or_row row_count:3>
Note that
f_in.headers
#=> ["name", "blah_01_blah", "foo_1_01_foo", "bacon_01_bacon", "bacon_02_bacon"]
Let's look at the output file.
puts File.read(file_out)
prints
name,newblah1,foo_1_01_foo,goodpig1,bacon_02_bacon
John,yucky,summer,yum,food
Mary,"","",cool,sundae

How do I make an array of arrays out of a CSV?

I have a CSV file that looks like this:
Jenny, jenny#example.com ,
Ricky, ricky#example.com ,
Josefina josefina#example.com ,
I'm trying to get this output:
users_array = [
['Jenny', 'jenny#example.com'], ['Ricky', 'ricky#example.com'], ['Josefina', 'josefina#example.com']
]
I've tried this:
users_array = Array.new
file = File.new('csv_file.csv', 'r')
file.each_line("\n") do |row|
puts row + "\n"
columns = row.split(",")
users_array.push columns
puts users_array
end
Unfortunately, in Terminal, this returns:
Jenny
jenny#example.com
Ricky
ricky#example.com
Josefina
josefina#example.com
Which I don't think will work for this:
users_array.each_with_index do |user|
add_page.form_with(:id => 'new_user') do |f|
f.field_with(:id => "user_email").value = user[0]
f.field_with(:id => "user_name").value = user[1]
end.click_button
end
What do I need to change? Or is there a better way to solve this problem?
Ruby's standard library has a CSV class with a similar api to File but contains a number of useful methods for working with tabular data. To get the output you want, all you need to do is this:
require 'csv'
users_array = CSV.read('csv_file.csv')
PS - I think you are getting the output you expected with your file parsing as well, but maybe you're thrown off by how it is printing to the terminal. puts behaves differently with arrays, printing each member object on a new line instead of as a single array. If you want to view it as an array, use puts my_array.inspect.
Assuming that your CSV file actually has a comma between the name and email address on the third line:
require 'csv'
users_array = []
CSV.foreach('csv_file.csv') do |row|
users_array.push row.delete_if(&:nil?).map(&:strip)
end
users_array
# => [["Jenny", "jenny#example.com"],
# ["Ricky", "ricky#example.com"],
# ["Josefina", "josefina#example.com"]]
There may be a simpler way, but what I'm doing there is discarding the nil field created by the trailing comma and stripping the spaces around the email addresses.

Resources