Top Level Namespace
Defined Under Namespace
Modules: FBayesDir
Classes: Categories, Food, InternalTurnk, USDA
Constant Summary
collapse
- FILES_ENUM =
{
"Food Description" => "FOOD_DES",
"Nutrient Data" => "NUT_DATA",
"Weight" => "WEIGHT",
"Footnote" => "FOOTNOTE",
"Food Group Description" => "FD_GROUP",
"LanguaL Factor" => "LANGUAL",
"LanguaL Factors Description" => "LANGDESC",
"Nutrient Definition" => "NUTR_DEF",
"Source Code" => "SRC_CD",
"Data Derivation Description" => "DERIV_CD",
"Sources of Data" => "DATA_SRC",
"Sources of Data Link" => "DATSRCLN"
}
- FIELD_SEP =
','
- FIELD_ENC =
'~'
- NULL_FIELDS =
['^^','~~',"\r\n",'']
- DATA_PATH =
'lib/data/sr24/'
Instance Method Summary
collapse
Instance Method Details
#parse ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/data/sr24/convert_to_csv.rb', line 86
def parse
paths = {}
FILES_ENUM.each do |n,f|
f_path = DATA_PATH + f + ".txt"
paths[f] = parse_to_csv(f_path)
end
paths
end
|
#parse_line(line) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/data/sr24/convert_to_csv.rb', line 26
def parse_line(line)
line.chomp!("\r\n")
fields = line.split('^')
p_fields = []
fields.each do |field|
if field.index('"')
field.gsub!(/[\"]/,"'")
end
if field.index(',')
field.gsub!(/[\,]/,"\,")
end
if field.index(FIELD_ENC) and field.size > 2
p_fields.push field.chop!.reverse!.chop!.reverse!
else
if NULL_FIELDS.index(field)
p_fields.push ''
elsif field.index('.')
p_fields.push field.to_f
elsif field.index('/')
p_fields.push field
else
p_fields.push field.to_i
end
end
end
p_fields
end
|
#parse_to_csv(file_path) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/data/sr24/convert_to_csv.rb', line 60
def parse_to_csv(file_path)
file = open(file_path, 'r:windows-1251')
puts file_path
rows = []
file.each do |line|
rows.push parse_line(line)
end
csv_data = rows.collect { |row| row.join('^') }.join("\n")
txt_ext_pop= /^(.+)\.txt/
csv_file_path = txt_ext_pop.match(file_path)[1] + '.csv' puts csv_file_path
csv_file = File.new(csv_file_path, 'w')
csv_file.write(csv_data)
csv_file.close
csv_file_path
end
|