python读取文件的列数
时间: 2024-04-19 11:22:41 浏览: 94
python可以统计文件的行数
以下是两种Python读取文件列数的例子:
1. 读取Excel文件的列数[^1]:
```python
import csv
input_path = r"D:\\"
input_csv = csv.reader(open(input_path + "xxx.csv", 'r'))
row_num = ""
for row in input_csv:
row_num = row
print(len(row_num)) # 输出列数
```
2. 读取txt文件的某一列数据[^2]:
```python
list1 = []
try:
file = open('test.txt', 'r')
except FileNotFoundError:
print('File is not found')
else:
lines = file.readlines()
for line in lines:
a = line.split()
x = a # 读取第5列数据
list1.append(x)
file.close()
for x in list1:
print(x)
```
阅读全文