In python, there are many ways to read data. Here are some common methods:
file = open("data.txt", "r")# 打开文件 data = file.read()# 读取文件中的数据 file.close()# 关闭文件
with open("data.txt", "r") as file: data = file.read()# 读取文件中的数据
import csv with open("data.csv", "r") as file: csv_reader = csv.reader(file)# 创建CSV读取器 for row in csv_reader: print(row)# 打印每一行数据
import json with open("data.json", "r") as file: data = json.load(file)# 加载JSON数据
These methods can be selected and adjusted according to different data formats and needs.
The above is the detailed content of How to read data in python. For more information, please follow other related articles on the PHP Chinese website!