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

質問編集履歴

1

test

2018/05/01 21:30

投稿

fujiji
fujiji

スコア6

title CHANGED
@@ -1,1 +1,1 @@
1
- python3でのunicodedecodeerror
1
+ unicodedecodeerror
body CHANGED
@@ -1,65 +1,4 @@
1
1
  ###前提・実現したいこと
2
2
  pythonでファイルを読み込もうとする際にunicodedecodeerrorが生じるのですが解決方法がわかりません
3
3
 
4
- ###発生している問題・エラーメッセージ
4
+ ###発生している問題・エラーメッセージ
5
-
6
- ```
7
- Traceback (most recent call last):
8
- File "d2v.py", line 46, in <module>
9
- lines = f.readlines()
10
- File "/Users/admin/.pyenv/versions/3.5.0/lib/python3.5/codecs.py", line 321, in decode
11
- (result, consumed) = self._buffer_decode(data, self.errors, final)
12
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
13
- ```
14
-
15
- ###python
16
- ```ここに言語を入力
17
- import os
18
- import sys
19
- import MeCab
20
- import collections
21
- from gensim import models
22
- from gensim.models.doc2vec import LabeledSentence
23
- from gensim.models.doc2vec import TaggedDocument
24
- import codecs
25
- import MeCab
26
-
27
- from gensim.models import doc2vec
28
-
29
- from gensim.models.doc2vec import Doc2Vec
30
-
31
-
32
-
33
- # 現在のディレクトリを取得
34
- root_dir = os.path.abspath(os.path.dirname(__file__))
35
-
36
-
37
- # ディレクトリ以下のテキストファイルを取得
38
- def getFileList(path):
39
- file_list = []
40
- for (root, dirs, files) in os.walk(path):
41
- for file in files:
42
- # パスセパレータは\より/の方が好きなので置換
43
- file_list.append( os.path.join(root,file).replace("\", "/") )
44
-
45
- return file_list
46
-
47
-
48
-
49
-
50
-
51
-
52
- # DS_Storeを省く
53
- all_files = getFileList(root_dir)[2:]
54
-
55
-
56
- print(all_files)
57
-
58
-
59
- all_sentences = []
60
- for file in all_files:
61
- f = open(file, encoding='utf-8')
62
- lines = f.readlines()
63
- for line in lines:
64
- all_sentences.append(line.replace('\n',''))
65
- ```