K M*_*lam 6 ruby csv delimiter delimited-text
我需要能够在我的Ruby项目中找出csv文件(逗号,空格或分号)中使用的分隔符.我知道,csv模块中的Python中有一个Sniffer类可用于猜测给定文件的分隔符.Ruby中有类似的东西吗?非常感谢任何形式的帮助或想法.
Gar*_*ver 12
看起来py实现只是检查几个方言:excel或excel_tab.所以,只是检查","或是的一个简单的实现"\t":
COMMON_DELIMITERS = ['","',"\"\t\""]
def sniff(path)
first_line = File.open(path).first
return nil unless first_line
snif = {}
COMMON_DELIMITERS.each {|delim|snif[delim]=first_line.count(delim)}
snif = snif.sort {|a,b| b[1]<=>a[1]}
snif.size > 0 ? snif[0][0] : nil
end
Run Code Online (Sandbox Code Playgroud)
注意:这会返回它找到的完整分隔符,例如",",这样可以让,你更改snif[0][0]为snif[0][0][1].
另外,我正在使用,count(delim)因为它有点快,但是如果你添加了一个由两个(或更多)相同类型的字符组成的分隔符--,那么在称重类型时它可能每次出现两次(或更多) ,所以在这种情况下,使用它可能更好scan(delim).length.
这是Gary S. Weaver在生产中使用它的答案.好的解决方案,运作良好.
class ColSepSniffer
NoColumnSeparatorFound = Class.new(StandardError)
EmptyFile = Class.new(StandardError)
COMMON_DELIMITERS = [
'","',
'"|"',
'";"'
].freeze
def initialize(path:)
@path = path
end
def self.find(path)
new(path: path).find
end
def find
fail EmptyFile unless first
if valid?
delimiters[0][0][1]
else
fail NoColumnSeparatorFound
end
end
private
def valid?
!delimiters.collect(&:last).reduce(:+).zero?
end
# delimiters #=> [["\"|\"", 54], ["\",\"", 0], ["\";\"", 0]]
# delimiters[0] #=> ["\";\"", 54]
# delimiters[0][0] #=> "\",\""
# delimiters[0][0][1] #=> ";"
def delimiters
@delimiters ||= COMMON_DELIMITERS.inject({}, &count).sort(&most_found)
end
def most_found
->(a, b) { b[1] <=> a[1] }
end
def count
->(hash, delimiter) { hash[delimiter] = first.count(delimiter); hash }
end
def first
@first ||= file.first
end
def file
@file ||= File.open(@path)
end
end
Run Code Online (Sandbox Code Playgroud)
规格
require "spec_helper"
describe ColSepSniffer do
describe ".find" do
subject(:find) { described_class.find(path) }
let(:path) { "./spec/fixtures/google/products.csv" }
context "when , delimiter" do
it "returns separator" do
expect(find).to eq(',')
end
end
context "when ; delimiter" do
let(:path) { "./spec/fixtures/google/products_with_semi_colon_seperator.csv" }
it "returns separator" do
expect(find).to eq(';')
end
end
context "when | delimiter" do
let(:path) { "./spec/fixtures/google/products_with_bar_seperator.csv" }
it "returns separator" do
expect(find).to eq('|')
end
end
context "when empty file" do
it "raises error" do
expect(File).to receive(:open) { [] }
expect { find }.to raise_error(described_class::EmptyFile)
end
end
context "when no column separator is found" do
it "raises error" do
expect(File).to receive(:open) { [''] }
expect { find }.to raise_error(described_class::NoColumnSeparatorFound)
end
end
end
end
Run Code Online (Sandbox Code Playgroud)