Pythonベース5
Pythonの異常処理,ファイル処理ログ処理に関する概念を紹介し,データ処理の様々な方法をまとめた.
ダウンタイムなしでプログラムを実行するために、発生する可能性のある例外を事前に処理します.
文法以外 try文エラー発生時に除外文を実行 Exceptionはすべてのエラーを見つけることができます. 複数の扉がある場合は上から調査を行い、異常が発生した場合は処理される 「as」文を使用してエラー文を出力できます
try~を除く(+)構文 try~を除く~els:try構文誤りなし try~exception~finally:tryまたはexception構文完了後に実行
フレーズ ユーザーが必要とするときに例外が発生する可能性があります
アサーション文 後の条件がTrueでない場合、AssertErrorが生成される.(関数のタイプチェックなどです.<ヒントを使用する方法もあります>)
ファイルオープンモードのタイプ
ファイルの取得
ファイル読み込み(readlines):コンテンツを一度に取得
ファイル読み込み(readline):行ごとにコンテンツを取得する(大きなファイルの処理に役立ち、終了条件を設定する必要がある)
ファイルをエンコーディングで書き込む
pickle bullit-inオブジェクト(オブジェクトの情報をpickleファイルとして保存) 格納する必要がある情報、計算結果(モデル)など.
print()とは異なり、ログモジュールはどのようなメッセージ(単純情報、エラーなど...)判断しやすいのでloggigモジュールを使用できます.また、プログラムの進捗状況を残すために、コンテンツをファイルに保存することもできます.
osモジュールのpath.joinを使用してオペレーティングシステムに適したパスをエクスポート
ファイルのコピー
pathlibモジュールを使用すると、pathをオブジェクトとしてより容易にアクセスできます.
必要なファイルに保存する文字列(文)を保存
ログ処理:ログモジュールを使用して基本ログを管理できます.
デフォルトでは、初期設定は警告レベルから出力されます
ステップ制御には追加の設定が必要です
ログの内容はファイルに保存できます
フォーマットアプリケーション:ログのフォーマットを指定できます
ファイルの場所、格納場所、操作タイプなどの項目で重複して使用する設定情報を管理するため、Pythonは1)configparser 2)argpparserを使用する
configparser:プログラム設定情報をファイルに挿入し、モジュールの値を使用します.
端末のtouchコマンドを使用してファイルを作成し、viコマンドでファイルを変更します.
-argparser(Command-Line Option):コンソールでプログラム実行時の設定を提供し、保存します.
データストアタイプ csv Webファイル XMLファイル JSONファイル csv(comma separate Values) フィールドをカンマで区切ったテキストファイル EXCELテーブルのデータは、プログラムを記述するために作成された Webファイル Web上のhtmlは、通常、特定のデータをスクロールするために使用される 正規表現を使用して必要なデータを抽出できます |通常のテストhttps://regexr.com/で行うことができます XML 記述データ構造と意味をTAGで表示 TAG間提供構造化情報 アーキテクチャやDTD(Document Type Definition)などの情報を表すメタ情報により、複数の形状に変換可能 htmlは文法に似ています. 複数モジュール対応(Ex BeautifulSoup)
JSON(JavaScript Object Notation) 当初はjsdmlデータオブジェクト表現法であったが、その構造が分かりやすいため、XMLの代替品として一般的に用いられている Pythonのdictタイプと互換性 各種API使用時にデータ伝送間で多くのデータ構造を使用
Naver BoostCap AI技術と課程
https://m.blog.naver.com/PostView.nhn?blogId=acornedu&logNo=220934409189&proxyReferer=https:%2F%2Fwww.google.com%2F
https://wikidocs.net/21050
https://wikidocs.net/26
https://tariat.tistory.com/844
https://docs.python.org/ko/3/library/argparse.html
https://docs.python.org/ko/3.7/howto/logging-cookbook.html
https://docs.python.org/ko/3/howto/logging-cookbook.html
Pythonの異常処理
ダウンタイムなしでプログラムを実行するために、発生する可能性のある例外を事前に処理します.
文法以外



try~を除く(+)構文


フレーズ

アサーション文

PythonのファイルI/O
ファイルオープンモードのタイプ

ファイルの取得

ファイル読み込み(readlines):コンテンツを一度に取得

ファイル読み込み(readline):行ごとにコンテンツを取得する(大きなファイルの処理に役立ち、終了条件を設定する必要がある)

ファイルをエンコーディングで書き込む

pickle



Pythonログを残す
osモジュールのpath.joinを使用してオペレーティングシステムに適したパスをエクスポート

ファイルのコピー

pathlibモジュールを使用すると、pathをオブジェクトとしてより容易にアクセスできます.

必要なファイルに保存する文字列(文)を保存

ログ処理:ログモジュールを使用して基本ログを管理できます.
デフォルトでは、初期設定は警告レベルから出力されます

ステップ制御には追加の設定が必要です

ログの内容はファイルに保存できます


フォーマットアプリケーション:ログのフォーマットを指定できます
#log formatter
#log의 결과값의 format을 지정해줄 수 있음
import logging
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('spam.log') #파일에 로그 저장
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler() #콘솔에 찍히는 로그 레벨 저장
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
logger.debug("a")
logger.info("b")
logger.warning("c")
logger.error("d")
logger.critical ("e")

Pythonの設定の管理
ファイルの場所、格納場所、操作タイプなどの項目で重複して使用する設定情報を管理するため、Pythonは1)configparser 2)argpparserを使用する
configparser:プログラム設定情報をファイルに挿入し、モジュールの値を使用します.
端末のtouchコマンドを使用してファイルを作成し、viコマンドでファイルを変更します.


-argparser(Command-Line Option):コンソールでプログラム実行時の設定を提供し、保存します.
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args)
print(args.accumulate(args.integers))
python3 argparser_ex.py 1 2 3 4

python3 argparser_ex.py 1 2 3 4 --sum

python3 argparser_ex.py -h

データの格納方式
with open("csv/customers.csv") as f:
while True:
data = f.readline() #한줄씩 가져옴
if not data:
break
import csv
with open("csv/customers.csv") as f:
#delimiter : 필드의 구분, quotechar : delimiter를 포함한 데이터를 묶을 구분자, quote의 필드 적용범위<csv.QUOTE_ALL - 모든 필드가 '"'로 구분>
reader = csv.reader(f,delimiter = ',', quotechar = '"', quoting = csv.QUOTE_ALL)
import re
import urllib.request
#html내용을 가져옴
url = "https://bit.ly/3rxQFS4"
html = urllib.request.urlopen(url)
html_contents = str(html.read().decode("utf8"))
results = re.findall(r"([A-Za-z0-9]+\*\*\*)", html_contents) #패턴을 가진 문자열을 찾아냄
for result in results:
print(result)




Reference
Naver BoostCap AI技術と課程
https://m.blog.naver.com/PostView.nhn?blogId=acornedu&logNo=220934409189&proxyReferer=https:%2F%2Fwww.google.com%2F
https://wikidocs.net/21050
https://wikidocs.net/26
https://tariat.tistory.com/844
https://docs.python.org/ko/3/library/argparse.html
https://docs.python.org/ko/3.7/howto/logging-cookbook.html
https://docs.python.org/ko/3/howto/logging-cookbook.html
Reference
この問題について(Pythonベース5), 我々は、より多くの情報をここで見つけました https://velog.io/@ganta/파이썬-기초5テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol