easy_sheet_io 0.3.6 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasySheetIo
4
- VERSION = "0.3.6"
4
+ VERSION = "0.3.8"
5
5
  end
data/lib/easy_sheet_io.rb CHANGED
@@ -19,7 +19,8 @@ module EasySheetIo
19
19
 
20
20
  # ##Generate Array from CSV File, and convert it to Hash or DataFrame.
21
21
  # **opt candidate= line_from: 1, header: 0
22
- def read_csv(path, format: nil, encoding: "utf-8", col_sep: ",", **opt)
22
+ # ver. 0.3.8~ default format=:daru
23
+ def read_csv(path, format: :daru, encoding: "utf-8", col_sep: ",", **opt)
23
24
  # Get 2D Array
24
25
  begin
25
26
  csv = CSV.parse(File.open(path, encoding: encoding, &:read), col_sep: col_sep)
@@ -29,21 +30,29 @@ module EasySheetIo
29
30
  csv = CSV.parse(File.open(path, encoding: "cp932", &:read), col_sep: col_sep)
30
31
  end
31
32
 
32
- return csv if format.nil?
33
-
34
- # Convert Hash or DataFrame
35
- ans = to_hash(csv, **opt)
36
- return format==:hash || format=="hash" ? ans : to_df(ans, format: format)
33
+ if format.to_s == "array"
34
+ return csv
35
+ elsif format.to_s == "hash"
36
+ return to_hash(csv, **opt)
37
+ else # include format.nil?
38
+ ans = to_df(to_hash(csv, **opt), format: format)
39
+ ans.convert_enc!(from: encoding, to: "utf-8") if encoding != "utf-8"
40
+ return ans
41
+ end
37
42
  end
38
43
 
39
44
  # ##Generate Array from EXCEL File, and convert it to Hash or DataFrame.
40
45
  # **opt candidate= line_from: 1, header: 0)
41
46
  def read_excel(path, sheet_i: 0, format: nil, encoding: "utf-8", **opt)
42
47
  a2d = open_excel(path, sheet_i, encoding: encoding) # Get 2D Array
43
- return a2d if format.nil?
44
-
45
- ans = to_hash(a2d, **opt)
46
- return format==:hash || format=="hash" ? ans : to_df(ans, format: format)
48
+
49
+ if format.to_s == "array"
50
+ return a2d
51
+ elsif format.to_s == "hash"
52
+ return to_hash(a2d, **opt)
53
+ else # include format.nil?
54
+ return to_df(to_hash(a2d, **opt), format: format)
55
+ end
47
56
  end
48
57
 
49
58
  # Convert 2d Array to Hash
data/lib/to_csv.rb CHANGED
@@ -14,15 +14,32 @@ class Daru::DataFrame
14
14
  return ans
15
15
  end
16
16
 
17
- def write_csv(path)
18
- open(path, "w") { _1.write to_csv }
17
+ def write_csv(path, encoding: nil)
18
+ enc = encoding.nil? ? "" : ":#{encoding}"
19
+ open(path, "w#{enc}") { _1.write to_csv }
19
20
  end
21
+
22
+ # To avoid bug about adding column to Daru::DataFrame
23
+ def add_vector(vecname, vec)
24
+ self[vecname] = vec
25
+ self.rename_vectors({vecname => vecname})
26
+ end
27
+
28
+ # ver.0.3.8~ Convert Daru::DF encoding
29
+ def convert_enc!(from: "cp932", to: "utf-8")
30
+ self.vectors.each do |col|
31
+ self[col] = self[col].each {|val| val.encode!(to, from_encoding: from) } if self[col][0].is_a?(String)
32
+ end
33
+ end
34
+
35
+ alias_method :addvec, :add_vector
20
36
  end
21
37
 
22
38
  class Rover::DataFrame
23
39
  # Rover#to_csv is already exist.
24
40
 
25
- def write_csv(path)
26
- open(path, "w") {|f| f.write self.to_csv}
41
+ def write_csv(path, encoding: nil)
42
+ enc = encoding.nil? ? "" : ":#{encoding}"
43
+ open(path, "w#{enc}") {|f| f.write self.to_csv}
27
44
  end
28
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_sheet_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - show-o-atakun
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-25 00:00:00.000000000 Z
11
+ date: 2023-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: daru
@@ -97,6 +97,7 @@ files:
97
97
  - LICENSE.txt
98
98
  - README.md
99
99
  - Rakefile
100
+ - Test.csv
100
101
  - bin/console
101
102
  - bin/setup
102
103
  - easy_sheet_io.gemspec