跳转至

File

CSV File

Number of columns in CSV file

Python
1
2
3
4
5
6
7
8
9
import csv

with open('example.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    # 假设文件的第一行是标题行  
    headers = next(reader)  
    # 文件的列数等于标题行的元素个数  
    num_columns = len(headers)  
    print(f'列数: {num_columns}')

csv file

Text Only
1
2
3
1/2/2014,5,8,red
1/3/2014,5,2,green
1/4/2014,9,1,blue

output:

Text Only
1
2
PS D:\work\python_work\ModernPython\codes\csv\columns> py.exe .\testprj.py
列数: 4

print

Python
1
2
3
ni=10
nj=20
print(f'ni={ni},nj={nj}')
Text Only
1
2
PS D:\work\python_work\ModernPython\codes\print\01> python .\testprj.py
ni=10,nj=20