在Python中,有多種方法可以讀取數(shù)據(jù)。以下是一些常見的方法:
file = open("data.txt", "r") # 打開文件
data = file.read() # 讀取文件中的數(shù)據(jù)
file.close() # 關(guān)閉文件
with open("data.txt", "r") as file:
data = file.read() # 讀取文件中的數(shù)據(jù)
import csv
with open("data.csv", "r") as file:
csv_reader = csv.reader(file) # 創(chuàng)建CSV讀取器
for row in csv_reader:
print(row) # 打印每一行數(shù)據(jù)
import json
with open("data.json", "r") as file:
data = json.load(file) # 加載JSON數(shù)據(jù)
這些方法可以根據(jù)不同的數(shù)據(jù)格式和需求進行選擇和調(diào)整。