現在発生している問題(どこまでできているか)と分からない(できていない)ことを具体的に追記ください。
前提・実現したいこと
csvファイルには1行ごとに英語の文章があります。
reviewディレクトリ内のcsvファイルを一つずつ読み込み
csvファイル内にgreatが含まれている文章だけを残して別のディレクトリに保存を行いたいです。
該当のソースコード
python
1# -*- coding: utf-8 -*- 2# 単語の出現頻度をExcelに 3 4import openpyxl 5import os 6import codecs 7import glob 8from collections 9 10def Treetaggerwrapper_en(spotname): 11 try: 12 word = [] # 単語 13 try: 14 fr = codecs.open(spotname, "rb", "sjis", "ignore") #読み込み 15 texts = fr.read().split("\n") #ファイルを1行ずつリストとしてtextに 16 fr.close() 17 texts = list(filter(lambda a: a != "", texts)) #空リスト削除 18 debug = 0 19 for text in texts: 20 debug +=1 21 if debug%50 == 0: 22 print("open_file = " + spotname + " lines = " + str(debug)) 23 word = word + treetagger_en.keitaiso_kaiseki(text.replace("\s", " ")) #分かち書きしたワード 24 25 except Exception: 26 import traceback 27 traceback.print_exc() 28 29 except Exception: 30 import traceback 31 traceback.print_exc() 32 33def Save_File_en2(data, save_directory, save_filename): 34 #ファイルに書き込み 35 wb = openpyxl.Workbook() 36 ws = wb.active 37 38 num = 1 39 for k,v in sorted(data.items(),key=lambda x:x[1],reverse=True): 40 ws.cell(column=1, row=num).value=k # 単語(原型) 41 ws.cell(column=2, row=num).value=v # 出現回数 42 num += 1 43 44 wb.save(save_directory + save_filename) 45 print("Save_Complete!\n") 46 47 48if __name__ == '__main__': 49 path = os.getcwd() # このプログラムの場所 50 textrank_dir = "\textrank\" # "textrank"を保存するディレクトリ 51 review_dir = "\review\" # "review"ディレクトリ 52 if os.path.isdir(path + textrank_dir) == False: # "textrank"というフォルダが無ければ作成 53 os.mkdir(path + textrank_dir) 54 os.chdir("review/") 55 56 csv_files = glob.glob("*.csv") 57 save_directory = path + textrank_dir 58 59 csv_files_jp = [] 60 csv_files_en = [] 61 62 for j_or_e in csv_files: 63 if 'a' <= j_or_e[0:1] <= 'z' or 'A' <= j_or_e[0:1] <= 'Z': 64 csv_files_en.append(j_or_e) 65 else: 66 csv_files_jp.append(j_or_e)
あなたの回答
tips
プレビュー