Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
2020_k3_python講座第5回
Search
k3たっつー
June 27, 2020
Education
69
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
2020_k3_python講座第5回
2020年度のk3のpython講座第5回の内容になります
k3たっつー
June 27, 2020
More Decks by k3たっつー
See All by k3たっつー
2020_k3_python講座第8回
tattsu_hitsuzi
0
46
2020_k3_python講座第7回
tattsu_hitsuzi
0
42
2020_k3_python講座第6回
tattsu_hitsuzi
0
36
2020_k3_python講座第4回
tattsu_hitsuzi
0
35
2020_k3_python講座第3回
tattsu_hitsuzi
0
1.1k
2020_k3_python講座第2回
tattsu_hitsuzi
0
46
2020_k3_python講座第1回
tattsu_hitsuzi
0
58
Other Decks in Education
See All in Education
Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
3.5k
HTML5 and the Open Web Platform - Lecture 3 - Web Technologies (1019888BNR)
signer
PRO
3
3.6k
【新規入学者様】ZEPメタバース校舎ガイド
ainischool
0
200
BRIEF OUTLINE OF IMPORTANT PLANT CONSTITUENTS
pawan_pharm
0
210
Book-keeping
bvr7352
0
130
An Experiment in "Cross-Cultural Exchange" as a Method for Content Creation
naoji_taniguchi_ksk_0827
0
250
Carbohydrates: Classification, Structure & Chemical Properties
pawan_pharm
0
330
Sanapilvet opetuksessa
matleenalaakso
1
36k
DRUGS ACTING ON THE PERIPHERAL NERVOUS SYSTEM
pawan_pharm
0
160
Requirements Analysis and Prototyping - Lecture 3 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
1.8k
CLASSIFICATION OF CRUDE DRUGS
pawan_pharm
0
220
[2026後期火5] 論理学(京都大学文学部 後期 第1回)「名前はあるのに、誰なのかわからない」
yatabe
0
150
Featured
See All Featured
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Embracing the Ebb and Flow
colly
88
5.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
58k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.2k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
320
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
We Are The Robots
honzajavorek
0
400
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
280
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
410
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Transcript
K3 Python講座 6月27日 担当:そすんさー 今日のテーマ:ファイルの読み書き
前回までのおさらい 関数 関数の定義 → 組み込み関数 → print()等、あらかじめあるもの
変数のスコープ → ローカル変数、グローバル変数 再帰関数 → 処理の中で自分を呼び出す関数
復習問題(L5_0.py) 3×3の行列式の値を求める関数を作成しよう M=[[1,-2,5],[6,-3,-1],[0,1,4]] |a b c| |d e
f |= a*e*i + b*f*g + c*h*d |g h i | - a*h*f – b*d*i - c*e*g
復習問題回答例 行列を多重リストとして みることで計算可能
今回のテーマ ファイル操作 ファイル操作とは? ファイルの読み込み ファイルパス
ファイルの書き込み ファイル操作
ファイル操作とは? Pythonを使って他のファイルを取り扱える Python ファイル (txt,csv, mp4等) ファイルをオープン ファイルオブジェクト が渡される
ファイル オブジェクト ファイルオブジェクト を介して、ファイル操 作 使い終わったらクローズ
ファイルの読み出し(1) 2種類の方法 open関数 変数名 = open(“ファイルのパス”) → 変数名.close()
が必要 with open with open(r“ファイルのパス”) as 変数名: 動作… → close処理を省略できる → ファイルのパスとはファイルの場所のこと → こっちを使えればOK
ファイルの読み出し(2) 変数名.read() → ファイルから全ての行を読み込む 変数名.readline() → ファイルから一行読み込む
変数名.readlines() → ファイル全体を行ごとに分割したリストにできる
ファイル読み出し(3) <文字列>.rstrip(“¥n”) →改行文字の削除が可能 <文字列>.split(“引数”) →文字列を引数で区切ることが出来る for line
in file: →ファイルの中身を一文ずつ見ることが出来る
ファイルの読み出し(4) 上の方法を組み合わせると、 リストの作成ができる!
ファイルの読み出し(5) 注意 →ファイルを読み込みと、 改行をしたところに ¥n が入っている 改行の原因
ファイルパス with open(r“ファイルのパス”) as 変数名: 動作… ファイルのパスには、相対パスと絶対パスがある
絶対パス →ドライブなど頂点からファイル名を示す 相対パス →作業フォルダからファイル名を示す
ファイルパス(2) 同じフォルダならファイル名でOK 絶対パスの求め方 ファイルのプロパティから見るのもOK
例題1 (L5_1.py) チャットに投げたファイルの中身を取得し、 turtleを用いて実行、描画しよう ファイルの場所は多分ダウンロード ダウンロードの中にある L5_1_turtle.txtを右クリック→プロパティ
例題回答
ファイルの書き込み with open with open(r“<ファイルのパス>”,mode = “モード名”) as 変数名
動作… モード名: “r” : 読み込みのみ “w” : 書き込み(既存ファイルに上書き) “a” : 書き込み(既存ファイルに追記) “x” : 書き込み(既存ファイルがあるならエラー) “+” : 読み書き両方したいとき 変数名.write(引数) → ファイルに引数を書き込む
例題2 (L5_2.py) 今から投げるテキストファイルの中身の形を 変えて、新しいテキストファイルに書き込もう ヒント:リスト
例題回答
encodingについて パソコンが読み込むのは、あくまで数字 文字コードと呼ばれる、文字を表す番号で識別 文字コードには種類がある →文字コードが違うと、正しく読みだせない、もしくはエラー
まとめ ファイル操作とは、pythonから他のファイルを操作 するということ
閑話 音楽の再生 https://techacademy.jp/magazine/20870 ぷよぷよプログラミング https://puyo.sega.jp/program_2020/
演習 (L5_3.py) To doリストを作成して保存するプログラムの作成 ファイルの中身を取得する機能 ファイルの中身を見る機能
ファイルに追加していく機能 ファイルの中身を消去する機能 ファイルの中身の優先順位を変更する関数
演習回答例
参考文献 https://www.sejuku.net/blog/24672 https://note.nkmk.me/python-file-io-open-with/ https://dot-blog.jp/news/python-open-function/