Last active
February 28, 2020 07:50
-
-
Save masao/10db0c36da39f23106a70518be4cf926 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "csv" | |
if ARGV.size == 0 | |
puts "USAGE: #$0 filename.txt" | |
exit | |
end | |
CSV.open(ARGV[0], "rb:UTF-16:UTF-8", col_sep: "\t", headers: true, quote_char: "\000") do |csv| | |
csv.each do |row| | |
#p row | |
data = {} | |
row.each do |k, v| | |
#p [k,v] | |
key = k.strip.downcase.intern | |
val = v.to_s.strip | |
data[key] = val | |
case key | |
when :id | |
data[:ncid] = val | |
when :bibinf | |
data[:bibinf].scan(/([A-Z0-9]+)=(.*?)(?= [A-Z0-9]+=|<\/?[A-Z0-9]+>)|\<\/?[A-Z0-9]+\>/).each do |element| | |
if not element.first.nil? | |
#p element | |
key2 = element[0].strip.downcase.intern | |
val2 = element[1].strip | |
#p [key2, val2] | |
case key2 | |
when :trd | |
data[:trd_title], data[:trd_author], = val2.split(/ \/ /) | |
end | |
data[key2] = val2 | |
end | |
end | |
end | |
end | |
next if data[:bibid].nil? or data[:bibid].empty? | |
if data[:vtd] and not data[:vtd].empty? | |
data[:other_title] = data[:vtd] | |
if data[:vtr] and not data[:vtr].empty? | |
data[:other_title] << "||" + data[:vtr] | |
end | |
end | |
data[:callno] = [ data[:cls], data[:bano] ].join("||") | |
if data[:volno] and not data[:volno].empty? | |
data[:callno] << "||" + data[:volno] | |
end | |
#p data | |
puts [:ncid, :bibid, :trd_title, :trd_author, :ed, | |
:pubp, :publ, :pubdt, :year1, :year2, | |
:physp, :physs, :trr, :other_title, | |
:voletc, :callno, :bookid | |
].map{|e| data[e] }.join("\t") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
year2を追加しました。