質問するログイン新規登録

回答編集履歴

2

e

2019/06/17 13:57

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  > エラーコードが出ているのを見るとos.pathで読み込んでいるものがファイルではなく
2
2
  DataFrameになってしまっている感じがするので、ファイル形式で読みめれば(多分)
3
3
 
4
- 推察されてる通り、read_csv("AAA.csv") で読み込んだ DataFrame を identification_filetype() に渡しているので、関数が受け取った引数 path がパスを表す文字列ではなくなっているのが原因です。
4
+ 推察されてる通り、read_csv("AAA.csv") で読み込んだ DataFrame を identification_filetype() に渡しているので、関数が受け取った引数がパスを表す文字列ではなくなっているのが原因です。
5
5
 
6
6
  identification_filetype() が指定したパスからファイルを読み込む関数を推定しているのであれば、以下のようにファイルパスを渡すようにして、関数内では読み込んだ DataFrame を return するべきではないでしょうか?
7
7
 
@@ -22,4 +22,31 @@
22
22
  os.chdir("C://Users//For Programming//Documents//Python Scripts3")
23
23
  df = identification_filetype("AAA.csv")
24
24
  df
25
+ ```
26
+
27
+ ## 補足
28
+
29
+ ちなみに read_table() は 0.24.0 で deprecated (廃止) された API なので、read_csv() で `sep="\t"` を指定して読み込むほうが好ましいです。
30
+
31
+ [pandas.read_table — pandas 0.24.2 documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_table.html)
32
+
33
+ ```python
34
+ import os
35
+
36
+ import pandas as pd
37
+
38
+
39
+ def identification_filetype(path):
40
+ _, ext = os.path.splitext(path)
41
+ if ext == ".csv":
42
+ # カンマ区切り
43
+ return pd.read_csv(path, encoding="shift-jis")
44
+ elif ext == ".txt":
45
+ # タブ区切り
46
+ return pd.read_csv(path, encoding="shift-jis", sep="\t")
47
+
48
+
49
+ os.chdir("C://Users//For Programming//Documents//Python Scripts3")
50
+ df = identification_filetype("AAA.csv")
51
+ df
25
52
  ```

1

2019/06/17 13:57

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -19,7 +19,7 @@
19
19
  return pd.read_table(path, encoding="shift-jis")
20
20
 
21
21
 
22
- #os.chdir("C://Users//For Programming//Documents//Python Scripts3")
22
+ os.chdir("C://Users//For Programming//Documents//Python Scripts3")
23
23
  df = identification_filetype("AAA.csv")
24
24
  df
25
25
  ```