1. 使用 `len()` 函数:
```python
with open('file.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
print(len(lines))
2. 使用 `enumerate()` 函数:
```python
with open('file.txt', 'r', encoding='utf-8') as file:
count = 0
for line in file:
count += 1
print(count)
3. 使用 `for-else` 语句:
```python
with open('file.txt', 'r', encoding='utf-8') as file:
for _ in file:
pass
else:
print("文件总行数:", file.tell())
4. 使用 `pandas` 库(如果你处理的是大型数据集):
```python
import pandas as pd
df = pd.read_csv('file.csv')
print(df.shape)
请根据你的具体需求选择合适的方法。如果你需要查看Excel文件中的行数,可以使用 `openpyxl` 库:
```python
import openpyxl
wb = openpyxl.load_workbook('file.xlsx')
ws = wb.active
print('工作表总行数:', ws.max_row)
以上代码将打开指定的Excel文件,并打印出工作表中的总行数。