1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

apache-beamで使ったpython標準メソッドまとめ

Last updated at Posted at 2024-03-10

はじめに

  • apache-beamを触ることになったがそもそもpythonを触ったことがあまりなかった。また似ている名前のメソッドもあり混同しそうだったため、備忘録としてまとめる。

pythonにおける変数のデータ型の確認type

str_data = 'hoge'
# <class 'str'>
print(type(str_data))

bytes_data = b'hoge'
# <class 'bytes'>
print(type(bytes_data))

int_data = 123
# <class 'int'>
print(type(int_data))

float_data = 123.456
# <class 'float'>
print(type(float_data))

dict_data = { "human": "hoge", "age": 23, "address": "shinjuku" }
# <class 'dict'>
print(type(dict_data))

文字列とバイト

str型からbytes型へencode

data = 'hoge'
encoded_data = data.encode()
# b'hoge'
print(encoded_data)

bytes型からstr型へdecode

data = b'hoge'
decoded_data = data.decode()
# hoge
print(decoded_data)

JSONとdict

そもそものjsonとdictの違い

  • JSONは「データフォーマット」であり、dictは「python」におけるデータ型
  • JSONを扱えるようにする(json[key_name]のようにアクセスするためなど)にはそれぞれのプログラミング言語ごと変換が必要
    • pythonにおいてはdict型に変換しなければならない
    • jsだったらparseしてオブジェクト型へ

str型からdict型へ変換loads

import json

data = '{ "name": "hoge", "age": 23, "address": "shinjuku" }'

dicted_data = json.loads(data)
# dict型に変換されているためkeyで抽出できる
# 23
print(dicted_data['age'])

# 以下の場合はエラーになる
# Traceback (most recent call last):
#   File "/app/src/loads.py", line 7, in <module>
#     print(data['age'])
# TypeError: string indices must be integers
print(data['age'])

dictをJSON(str)へ変換dumps

import json

data = { "human": "hoge", "age": 23, "address": "shinjuku" }

formatted_data = json.dumps(data)

# {"human": "hoge", "age": 23, "address": "shinjuku"}
print(formatted_data)
# <class 'str'>
print(type(formatted_data))

dictをJSONとしてファイルへ保存dump

import json

data = { "name": "hoge", "age": 23, "address": "shinjuku" }

with open('../result/index.json', 'w') as f:
    json.dump(data, f, indent=2)

出力結果

{
  "name": "hoge",
  "age": 23,
  "address": "shinjuku"
}

文字列の連結

イテレータの文字列を連結join

  • イテレータ...for文で回せるもの
  • list
  • tuple
  • str
  • dict
  • etc...
# list
list_data = ["f", "u", "g", "a"]
joined_list_data = "".join(list_data)
# fuga
print(joined_list_data)

# tuple
tuple_data = ("a","b","c","d")
joined_tuple_data = "".join(tuple_data)
# abcd
print(joined_tuple_data)

# str
str_data = ("hoge")
joined_str_data = "/".join(str_data)
# h/o/g/e
print(joined_str_data)

# dict
dict_data =  { "name": "hige", "age": 23, "address": "shinjuku" }
joined_dict_data = "/".join(dict_data)
# keyのみ結合される
# name/age/address
print(joined_dict_data)

# valueを結合したい場合
dict_data2 =  { "name": "higa", "age": 24, "address": "shibuya" }
joined_dict2_data = "/".join(str(value) for value in dict_data2.values())
# higa/24/shibuya
print(joined_dict2_data)

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?