- 1. Pythonでcsvにスペース区切りのリストを書き込む
- 2. Pythonでよく使うこと。コード、コマンド、スクレイピング、Anaconda、selenium
- 3. OpenPyxlの備忘録
- 4. pythonでExcel操作を自動化する
- 5. Windowsでpoetryが上手くインストールできない場合の対処
- 6. Python3 標準入力を辞書にする方法
- 7. PyScriptのすすめ
- 8. tensorflowがインストールできないだと!?
- 9. statsmodelsのseasonal_decomposeを活用した時系列データの可視化
- 10. CodeRunnerでpython: command not foundが出る場合
- 11. 【Pytorch】Transformerを実装する
- 12. 【Selenium】バージョン更新後のWarningについて
- 13. Pandas カスタム条件の関数でDataframeを比較する
- 14. [Python2 / Python3] python2 から python3 にアップデート / 移行した際のソース修正項目メモ
- 15. 【AtCoder解説】PythonでABC265のA,B,C,D,E問題を制する!
- 16. discordのイベント検知の方法
- 17. ローカル環境のStable Diffusionで大量に画像を生成して比較する
- 18. 【Python】陰陽座『廿弐匹目は毒蝮』をシミュレート
- 19. Stable DiffusionをGoogle Colaboratory上で動かして神絵ガチャを回してみる
- 20. 話題の Stable DiffusionをColabで作ってみた!
Pythonでcsvにスペース区切りのリストを書き込む
## 実現したいこと
リストを含むcsvをつくりたい ↓“`hoge.csv
a, b, [1 2 3], c
“`## 結論
これで実現できます。
“` hoge.py
l = [1, 2, 3] # リスト
l = ‘[‘ + ‘ ‘.join([str(x) for x in l]) + ‘]’ # リストをあらかじめ文字列にする!
row = [‘a’, ‘b’, l, ‘c’] # csvの1行# csvに出力
with open(‘hoge.csv’, ‘w’) as f:
writer = csv.writer(f)
writer.writerow(row)
“`## 失敗した方法
愚直に書いたコード ↓“`hoge.py
l = [1, 2, 3]
row = [‘a’, ‘b’, l, ‘c’] # csvの1行# csvに出力
with open(‘hoge.csv’, ‘w’) as f:
writer = csv.writer(f)
writer.writerow(row)
“`
Pythonでよく使うこと。コード、コマンド、スクレイピング、Anaconda、selenium
個人的によく使うことをメモしておきます。使うんだけど忘れちゃうので。
思いついたらどんどん加筆していきます。# 仮想環境をbaseに切り替えるコマンド
“`
$ conda activate
(base) $
“`参考)Conda コマンド
https://www.python.jp/install/anaconda/conda.html# Google ChromeとChromeDriverのバージョン不一致のとき
ChromeDriverのinstall“`
$ pip3 install chromedriver-binary==79.0.3945.36.0
“`参考)【Python/Selenium】ChromeDriverバージョンエラー対処法
https://yuki.world/python-chrome-driver-version-error/# seleniumのoption
headlessとか“`py
from selenium import webdriveroptions = webdriver.ChromeOp
OpenPyxlの備忘録
## はじめに
### 使用しているもの
`openpyxl 3.0.10`“`python
# 前提コード
import openpyxlwb = openpyxl.Workbook()
“`### 注意
間違いやもっとこうした方がいい、みたいのがあるかも## 内容
### get_なんたらは基本使わない
調べるとよく出てくる、
`wb.get_sheet_by_name(‘Sheet1’)`
なんかはもう古いみたい。
他にもなにやら新しくなっているものがあり、ちょいちょい怒られる。
以下新旧対照表
|内容|新|旧|
|—|—|—|
|シート名を指定する|wb[“Sheet1”]|wb.get_sheet_by_name(‘Sheet1’)|
|シートを消す|wb.remove(worksheet=wb[“Sheet1”])|wb.remove_sheet(wb.get_sheet_by_name(‘Sheet1’))|
|シート一覧の取得|wb.sheetnames|wb.get_sheet_names()|## おわりズイ₍₍ (ง ˘
pythonでExcel操作を自動化する
openpyxlを使用して、seleniumで取得したデータをExcelへ入力する作業を自動化します。
csvに出力する等もあると思いますが、Excelはどこでも使っていると思うのでExcelを選びました。## インストール
openpyxlを以下でインストールします
“`bash
$ pip install openpyxl
“`インストール確認は以下です。
“`bash
$ pip list | grep openpyxl
openpyxl 3.0.10
“`## ファイル読み込み
“`python
import openpyxl
wb = openpyxl.load_workbook(“読み込みたいファイル”)
“`## シート読み込み
“`python
sheet = wb[“読み込みたいシート名”]
“`## セル読み込み
“`python
value = sheet.cell(row=1, column=1).value
“`A1セルを読み込む
row=行、column=列
“`python
Windowsでpoetryが上手くインストールできない場合の対処
いつも通りPowerShellからpoetryを入れようとしたら入らなくなってた
“`PS
PS C:\Users\hoge> (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python –
Retrieving Poetry metadataThis installer is deprecated, and cannot install Poetry 1.2.0a1 or newer.
Additionally, Poetry installations created by this installer cannot `self update` to 1.2.0a1 or later.
You s
Python3 標準入力を辞書にする方法
先日、paizaラーニングのレベルアップ問題集で、連想配列の問題を解いていたときにでた疑問をまとめました。
https://paiza.jp/works/mondai/query_primer/query_primer__map_normal
(この練習問題の自分のコードも最後に載せてあります。)### python3 で 標準入力を辞書の形にする方法
まず、リストを作成し、そこに要素を順に入れていきます。
“`Python
list_dic = []for i in range(N):
list_dic.append(input().split())
“`
そして、“`dict()“`を使って辞書にします。“`Python
dictionary = dict(list_dic)
“`例:
標準入力
“`
1 Sin
2 Sakura
3 Kayo
4 Yui
“`コード
“`Python
mylist=[]for i in range(N):
mylist.append(input().split())mydict=d
PyScriptのすすめ
HTMLに直接Pythonが書ける画期的で革命的なライブラリのPyScriptについての情報を纏めます。
## 導入方法
ヘッダでJSライブラリをロードするだけです。
“`html