1. 程式人生 > >win10上用Python2.7處理文字,出錯IOError: [Errno 2] No such file or directory:如何解決???

win10上用Python2.7處理文字,出錯IOError: [Errno 2] No such file or directory:如何解決???

# coding=utf-8
import os
import jieba
import sys
import re
import time
import jieba.posseg as pseg

sys.path.append("../")
jieba.load_userdict("../Python27/fenci/dict.txt")  # 載入自定義分詞詞典

'''
title:利用結巴分詞進行文字語料處理:單文字處理器、批量檔案處理器
    1 首先對文字進行遍歷查詢
    2 建立原始文字的儲存結構
    3 對原文字進行結巴分詞和停用詞處理
    4 對預處理結果進行標準化格式,並儲存原檔案結構路徑
author:白寧超
myblog:http://www.cnblogs.com/baiboy/
time:2017年4月28日10:03:09
'''

'''
分詞.詞性標註以及去停用詞
stopwordspath: 停用詞路徑
dealpath:中文資料預處理檔案的路徑
savepath:中文資料預處理結果的儲存路徑
'''

"""
def cutTxtWord(dealpath, savepath, stopwordspath):
    stopwords = {}.fromkeys([line.rstrip() for line in open(stopwordspath, "r", encoding='utf-8')])  # 停用詞表
    with open(dealpath, "r", encoding='utf-8') as f:
        txtlist = f.read()  # 讀取待處理的文字
    words = pseg.cut(txtlist)  # 帶詞性標註的分詞結果
    cutresult = ""  # 獲取去除停用詞後的分詞結果
    for word, flag in words:
        if word not in stopwords:
            cutresult += word + "/" + flag + " "  # 去停用詞
            getFlag(cutresult, savepath)  #
"""

'''
分詞.詞性標註以及去停用詞
stopwordspath: 停用詞路徑
read_folder_path :中文資料預處理檔案的路徑
write_folder_path :中文資料預處理結果的儲存路徑
filescount=300 #設定資料夾下檔案最多多少個
'''


def cutFileWord(read_folder_path, write_folder_path, stopwordspath):
    # 停用詞表
    stopwords = {}.fromkeys([line.rstrip() for line in open(stopwordspath, "r", encoding='utf-8')])

    # 獲取待處理根目錄下的所有類別
    folder_list = os.listdir(read_folder_path)
    # 類間迴圈
    for folder in folder_list:
        # 某類下的路徑
        new_folder_path = os.path.join(read_folder_path, folder)

        # 建立儲存檔案目錄
        path = write_folder_path + folder  # 儲存檔案的子檔案
        isExists = os.path.exists(path)
        if not isExists:
            os.makedirs(path)
            print(path + ' 建立成功')
        else:
            pass
        save_folder_path = os.path.join(write_folder_path, folder)  # 某類下的儲存路徑
        print('--> 請稍等,正在處理中...')

        # 類內迴圈
        files = os.listdir(new_folder_path)
        j = 1
        for file in files:
            if j > len(files): break
            dealpath = os.path.join(new_folder_path, file)  # 處理單個檔案的路徑
            with open(dealpath, "r", encoding='utf-8') as f:
                txtlist = f.read()
                # python 過濾中文、英文標點特殊符號
                # txtlist1 = re.sub("[\s+\.\!\/_,$%^*(+\"\']+|[+——!,。?、
[email protected]
#¥%……&*()]+", "",txtlist) words = pseg.cut(txtlist) # 帶詞性標註的分詞結果 cutresult = "" # 單個文字:分詞後經停用詞處理後的結果 for word, flag in words: if word not in stopwords: cutresult += word + "/" + flag + " " # 去停用詞 savepath = os.path.join(save_folder_path, file) getFlag(cutresult, savepath) j += 1 ''' 做詞性篩選 cutresult:str型別,初切分的結果 savepath: 儲存檔案路徑 ''' def getFlag(cutresult, savepath): txtlist = [] # 過濾掉的詞性後的結果 # 詞列表為自己定義要過濾掉的詞性 cixing = ["/x", "/zg", "/uj", "/ul", "/e", "/d", "/uz", "/y"] for line in cutresult.split('\n'): line_list2 = re.split('[ ]', line) line_list2.append("\n") # 保持原段落格式存在 line_list = line_list2[:] for segs in line_list2: for K in cixing: if K in segs: line_list.remove(segs) break else: pass txtlist.extend(line_list) # 去除詞性標籤 resultlist = txtlist[:] flagresult = "" for v in txtlist: if "/" in v: slope = v.index("/") letter = v[0:slope] + " " flagresult += letter else: flagresult += v standdata(flagresult, savepath) ''' 標準化處理,去除空行,空白字元等。 flagresult:篩選過的結果 ''' def standdata(flagresult, savepath): f2 = open(savepath, "w", encoding='utf-8') for line in flagresult.split('\n'): if len(line) >= 2: line_clean = "/ ".join(line.split()) lines = line_clean + " " + "\n" f2.write(lines) else: pass f2.close() if __name__ == '__main__': t1 = time.time() # 測試單個檔案 # dealpath = "../Database/SogouC/FileTest/1.txt" # savepath = "../Database/SogouCCut/FileTest/1.txt" # stopwordspath = '../Database/stopwords/CH_stopWords.txt' stopwordspath1 = '../Python27/fenci/chstop.txt' # 哈工大停用詞表 # 批量處理資料夾下的檔案 # rfolder_path = '../Database/SogouC/Sample/' rfolder_path = '../Python27/fenci/FileNews/' # 分詞處理後儲存根路徑 wfolder_path = '../Python27/fenci/result/' # 中文語料前處理器 # cutTxtWord(dealpath,savepath,stopwordspath) # 單文字前處理器 cutFileWord(rfolder_path, wfolder_path, stopwordspath) # 多文字前處理器 t2 = time.time() print("中文語料語處理完成,耗時:" + str(t2 - t1) + "秒。") # 反饋結果
執行結果如下
Building prefix dict from the default dictionary ...
Loading model from cache c:\users\hp\appdata\local\temp\jieba.cache
Loading model cost 0.478 seconds.
Prefix dict has been built succesfully.
Traceback (most recent call last):
  File "D:/Python27/fenci/fenci4.py", line 10, in <module>
    jieba.load_userdict("../Python27/fenci/dict.txt")  # 載入自定義分詞詞典
  File "D:\Python27\lib\site-packages\jieba\__init__.py", line 374, in load_userdict
    f = open(f, 'rb')
IOError: [Errno 2] No such file or directory: '../Python27/fenci/dict.txt'


Process finished with exit code 1

相關推薦

win10Python2.7處理文字出錯IOError: [Errno 2] No such file or directory:如何解決???

# coding=utf-8 import os import jieba import sys import re import time import jieba.posseg as pseg sys.path.append("../") jieba.load_use

DataError: [-] Error: 2, No such file or directory解決Django寫項目使用Docker安裝FastDFS測試的時候遇到這個問題

安裝 tor 執行 項目 數據 docker serve error -- DataError: [-] Error: 2, No such file or directory我已經解決了 是因為我安裝storage的時候docker run -dti --network=

hyperscan-python時fatal error: Python.h: No such file or directory

1、centos7.3用hyperscan-python時報錯:fatal error: Python.h: No such file or directory 系統中沒有python.h的原因,是因為沒有安裝python的開發版,即Python-devel這個包,命

mysql 安裝問題二:mysqld: Can't create directory 'E:\Software\mysql-5.7.24-winx64\data\' (Errcode: 2 - No such file or directory)

原因:my.ini檔案中的basedir(設定mysql的安裝目錄)、datadir(設定mysql資料庫的資料的存放目錄)與MySQL解壓後的路徑不一致   解決辦法:   將basedir=E:\Software\mysql-5.7.24-winx64  改為&nbs

mingw下gcc編譯c檔案出現no such file or directory解決方法

c檔案直接拖進cmd時地址是對的,但gcc不認空格,所以要把路徑當做所有空格都去掉或改成“—”,這樣它就能直接發現檔案了,這時在cmd中編寫:gcc F:\new.c -o F:\new.exe   ,就會出現new.e

pip已經安裝但是/usr/bin/pip: No such file or directory

解決方法 1.which pip /usr/local/bin/pip 2.pip -su: /usr/bin/pip: No such file or directory 3.type pip pip is hashed (/usr/bin/pip) So p

切圖異常: Cannot run program "gm": error=2, No such file or directory 處理

切圖異常: Cannot run program “gm”: error=2, No such file or directory 處理 使用im4java、ImageMagick/GraphicsMagick進行切圖,結果在測試環境執行時提示gm找不到這個異

windows寫的指令碼ubuntu虛擬機器始終報錯 [Error 2]no such file or directory

切換到Mapping標籤,在Deployment path on server 'test'這一欄的最右側,點選...按鈕,選擇檔案上傳的目標目錄。之後點選Use this server as default,就可以讓這個工程一直預設上傳到這個伺服器的這個目錄,不用多次選擇

編譯核心出現gcc: error: elf_i386: No such file or directory

  CC      arch/x86/mm/mmio-mod.o   LD      arch/x86/mm/mmiotrace.o   LD      arch/x86/mm/built-in.o   CC      arch/x86/crypto/crc32c-inte

【G】win10安裝git後無法使用出現 fatal: open /dev/null or dup failed: No Such file or directory.

解決方案 解決方案一: 成功率較低 在C:\Windows\System32位置,找到cmd,以管理員執行cmd,貼上輸入sfc /scannow命令,進行修復,修復完重啟。 解決方案二: 成功率較高 替換C:\Windows\System32\driv

ubuntu下如何處理出現“ unable to execute ./DrClientLinux: No such file or directory“沒有那個檔案或目錄”的問題。

最近在重灌ubuntu的時候遇到了一個問題,因為在安裝的時候沒有連網,導致了沒有下載32位的庫,導致了沒法執行校園網的登入客戶端,因為那個客戶端是用32位寫的,所以出現了各種問題,經過L同學的幫助,最終弄好了,特意貼出程式碼,希望能夠幫助大家。千萬別遇到類似問題,不然會崩潰

Java 呼叫 FFMPEG 命令時 url 作為輸入源Linux 下出現 “no such file or directory” 問題的解決

        Windows 下執行 ffmpeg 命令,         D:/tools/ffmpeg/bin>ffmpeg.exe -i "某視訊檔案下載URL" -f flv D:/1.flv        可以成功直接將下載連結輸入源轉為 1.flv。

Mac mysql 忘記 root 密碼phpmyadmin 登入 No such file or directory 錯誤處理

20190629 mac 上安裝過 mysql 和 phpmyadmin之後,很久沒有使用(本機硬碟空間有限),今天想用的時候,死

R中讀取文件找不到路徑問題 No such file or directory

con tracking air csdn rac rect 路徑 路徑和 data R中讀取文件,找不到路徑問題 No such file or directory 近日,讀取文件時。出現例如以下問題 > passenger = read.c

cnmp安裝失敗報錯npm ERR! enoent ENOENT: no such file or directory,

lar 答案 .cn usr password rect tor bre -c 1.cnmp安裝失敗 2.提示如下: bogon:node_modules liangjingming$ sudo npm install cnpm -g --registry=https://

redhat7安裝jdk1.7報錯/home/renqiwei/jdk1.7/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory

ann .cn .so 解決方法 技術分享 cannot mage jdk1 i686 本地下載解壓安裝到/home/renqiwei/jdk1.7目錄下 root配置環境變量,報錯: 原因:是因為64位系統中安裝了32位程序 解決方法:yum install gli

ceph-create-keys: No such file or directory 處理

rip create tin trap admin exc bootstra rap str -- coding: utf-8 -- from future import unicode_literalsJan 15 21:42:47 l22-41-14 ceph-crea

激活miniconda2環境出現activate命令不存在的解決方案(activate: No such file or directory

director bsp ini nbsp update date pat -i min miniconda2版本比較低時會出現這種報錯,通過更新miniconda2就可以解決這個問題,用到的命令行: /path/to/miniconda2/conda update

pycharm 遠程環境時報錯bash: line 0: cd: /home/tmp: No such file or directory

charm rec 報錯 del let short mage arm lin delete redundant path pycharm 用遠程環境時報錯bash: line 0: cd: /home/tmp: No such file or directory

執行Linux腳本出現No Such file or directory異常

退出 linux服務器 腳本開發 win 不存在 存在 src 字符 lin 最近在學習Linux系統的腳本開發,在我編寫完標本的情況出現了以下的情況 這個時候出現了No Such file or directory 情況出現 ,提示文件不存在; 但是通過VIM命令訪問此