Python数据载入代码
时间: 2024-06-27 21:00:56 浏览: 93
数据上传代码
Python中用于数据载入的代码通常取决于你要读取的数据类型和文件格式。以下是一些常见的数据载入方法:
1. **CSV文件** (使用`pandas`库):
```python
import pandas as pd
data = pd.read_csv('filename.csv')
```
2. **Excel文件** (同样用pandas):
```python
data = pd.read_excel('filename.xlsx')
```
3. **JSON文件** (pandas或`json`模块):
```python
import json
with open('filename.json') as f:
data = json.load(f)
```
4. **文本文件** (逐行读取):
```python
with open('filename.txt', 'r') as file:
data = [line.strip() for line in file]
```
5. **SQL数据库** (使用`sqlite3`, `psycopg2`等库):
```python
import sqlite3
conn = sqlite3.connect('database.db')
cursor = conn.cursor()
data = cursor.execute("SELECT * FROM table_name").fetchall()
```
6. **CSV文件(异步加载)** (使用`dask`库):
```python
import dask.dataframe as dd
data = dd.read_csv('filename.csv')
```
7. **HDF5文件** (h5py库):
```python
import h5py
with h5py.File('filename.hdf5', 'r') as f:
data = f['dataset_name']
```
8. **CSV文件(Pandas DataFrame分块读取大型文件)**:
```python
chunksize = 10 ** 6 # 分块大小
chunks = []
for chunk in pd.read_csv('large_file.csv', chunksize=chunksize):
chunks.append(chunk)
data = pd.concat(chunks)
```
阅读全文