使用python读取外部数据源
时间: 2024-03-28 20:39:02 浏览: 29
可以使用Python中的各种库来读取外部数据源,例如:
1. csv模块:可以使用csv模块读取csv文件,例如:
```
import csv
with open('example.csv', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in reader:
print(', '.join(row))
```
2. pandas库:可以使用pandas库读取各种数据源,例如:
```
import pandas as pd
# 读取csv文件
df = pd.read_csv('example.csv')
# 读取Excel文件
df = pd.read_excel('example.xlsx')
# 读取JSON文件
df = pd.read_json('example.json')
# 读取数据库
import sqlite3
conn = sqlite3.connect('example.db')
df = pd.read_sql_query("SELECT * from example_table", conn)
```
3. requests库:可以使用requests库读取Web API接口返回的数据,例如:
```
import requests
response = requests.get('https://api.example.com/data')
data = response.json()
```