在Python中,load函數(shù)通常用于將數(shù)據(jù)從文件或字符串加載到內(nèi)存中,并將其轉(zhuǎn)換為Python對象。以下是load函數(shù)在數(shù)據(jù)轉(zhuǎn)換中的一些常見應(yīng)用方法:
import json
# 從文件中加載JSON數(shù)據(jù)
with open('data.json', 'r') as f:
data = json.load(f)
# 從字符串中加載JSON數(shù)據(jù)
data_str = '{"key": "value"}'
data = json.loads(data_str)
import yaml
# 從文件中加載YAML數(shù)據(jù)
with open('data.yaml', 'r') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# 從字符串中加載YAML數(shù)據(jù)
data_str = """
key: value
"""
data = yaml.safe_load(data_str)
import csv
# 從文件中加載CSV數(shù)據(jù)
with open('data.csv', 'r') as f:
reader = csv.DictReader(f)
data = list(reader)
import xml.etree.ElementTree as ET
# 從文件中加載XML數(shù)據(jù)
tree = ET.parse('data.xml')
root = tree.getroot()
這些是load函數(shù)在數(shù)據(jù)轉(zhuǎn)換中的一些常見應(yīng)用方法,具體應(yīng)用取決于加載的數(shù)據(jù)格式和數(shù)據(jù)結(jié)構(gòu)。