在Python中读取文件中的整数可以通过以下几种方法实现:
1. 使用正则表达式:
```python
import re
filename = 'CP_epoch264.pth'
regex = re.compile(r'\d+')
numbers = [int(x) for x in regex.findall(filename)]
print(max(numbers)) 如果文件名中只有一个数字,则使用max函数取最大值
2. 使用`input()`函数和`eval()`函数:
```python
user_input = input("请输入数字,用空格分隔:")
numbers = list(map(int, user_input.split()))
print(numbers)
3. 使用`open()`函数和`numpy`库:
```python
import numpy as np
with open('in.txt', 'r') as f:
rows, cols = np.fromfile(f, dtype=int, count=2, sep=' ')
data = np.fromfile(f, dtype=int, count=cols*rows, sep=' ').reshape((rows, cols))
print(data)
4. 使用`open()`函数和`map()`函数:
```python
with open('in.txt', 'r') as f:
data = []
cols, rows = map(int, f.readline().split())
for i in range(0, rows):
data.append(list(map(int, f.readline().split()[:cols])))
print(data)
请根据您的具体需求选择合适的方法。