投稿

ラベル(Python)が付いた投稿を表示しています

Sample of reading XML file with python

Sample of reading XML file with python import xml.etree.ElementTree as ET # Load the XML file tree = ET.parse('data.xml') root = tree.getroot() # Iterate through 'person' elements and extract data for person in root.findall('person'): name = person.find('name').text age = person.find('age').text print(f"Name: {name}, Age: {age}")

Sample of reading CSV file with python

CSVサンプル Name,Age Alice,25 Bob,30 Charlie,22 import csv def read_csv_file(file_path): try: with open(file_path, 'r', newline='') as csvfile: csv_reader = csv.DictReader(csvfile) # 各行のデータを利用 for row in csv_reader: name = row['Name'] age = int(row['Age']) print(f"{name} is {age} years old.") except FileNotFoundError: print("File not found.") except Exception as e: print("Error:", e) if __name__ == "__main__": csv_file_path = "example.csv" # CSVファイルのパスを指定してください read_csv_file(csv_file_path)

python で authorization header bearer token 方式のサンプル

import requests def main(): # 送信先のURL url = "https://example.com/api/endpoint" # Bearerトークン bearer_token = "your_bearer_token_here" # 送信するJSONデータ data = { "key1": "value1", "key2": "value2" } # POSTリクエストを送信 response = requests.post(url, json=data, headers={"Authorization": "Bearer " + bearer_token}) # レスポンスの内容を表示 if response.status_code == 200: print("Success!") print("Response:", response.json()) else: print("Error:", response.status_code, response.text) if __name__ == "__main__": main()

Python Post Request with Json Example

import requests import json def main(): # 送信先のURL url = "https://example.com/api/endpoint" # 送信するJSONデータ data = { "key1": "value1", "key2": "value2" } # JSONデータを文字列に変換 json_data = json.dumps(data) # POSTリクエストを送信 response = requests.post(url, data=json_data, headers={"Content-Type": "application/json"}) # レスポンスの内容を表示 if response.status_code == 200: print("Success!") print("Response:", response.json()) else: print("Error:", response.status_code, response.text) if __name__ == "__main__": main()