io模块的代码python
时间: 2024-02-04 12:03:54 浏览: 31
以下是io模块中常用函数的Python代码示例:
1. loadmat函数:
```python
import scipy.io
# Load MATLAB file
mat = scipy.io.loadmat('data.mat')
# Access variables in the file
data = mat['data']
labels = mat['labels']
# Print the loaded data
print(data)
print(labels)
```
2. savemat函数:
```python
import scipy.io
import numpy as np
# Create some data
data = np.array([[1, 2, 3], [4, 5, 6]])
# Create a dictionary to store the data
mat_dict = {'data': data}
# Save the data to a MATLAB file
scipy.io.savemat('data.mat', mat_dict)
```
3. loadtxt函数:
```python
import numpy as np
# Load data from a text file
data = np.loadtxt('data.txt')
# Print the loaded data
print(data)
```
4. genfromtxt函数:
```python
import numpy as np
# Load data from a text file
data = np.genfromtxt('data.txt', delimiter=',')
# Print the loaded data
print(data)
```
5. savetxt函数:
```python
import numpy as np
# Create some data
data = np.array([[1, 2, 3], [4, 5, 6]])
# Save the data to a text file
np.savetxt('data.txt', data, delimiter=',')
```
6. imread函数:
```python
import scipy.misc
# Load an image from a file
image = scipy.misc.imread('image.jpg')
# Print the image shape
print(image.shape)
```
7. imsave函数:
```python
import scipy.misc
# Create some data
data = np.array([[1, 2, 3], [4, 5, 6]])
# Save the data as an image
scipy.misc.imsave('image.png', data)
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)