Python3関連のことを調べてみた2022年09月03日

Python3関連のことを調べてみた2022年09月03日

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 webdriver

options = webdriver.ChromeOp

元記事を表示

OpenPyxlの備忘録

## はじめに
### 使用しているもの
`openpyxl 3.0.10`

“`python
# 前提コード
import openpyxl

wb = 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 metadata

This 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