import csv
with open('Football_data.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
dates = []
homeTeam = []
awayTeam = []
fullTimeHomeGoals = []
fullTimeAwayGoals = []
fullTimeResult = []
for row in readCSV:
#ignore row[0]
date = row[1]
home = row[2]
away = row[3]
FTHG = row[4]
FTAG = row[5]
FTR = row[6]
dates.append(date)
homeTeam.append(home)
awayTeam.append(away)
fullTimeHomeGoals.append(FTHG)
fullTimeAwayGoals.append(FTAG)
fullTimeResult.append(FTR)
print(date, home,FTHG, FTAG, away)
我正在尝试将CSV文件中的数据转换为整数。
我尝试过
FTHG = int(row[4])
FTAG = int(row[5])
但是出现以下错误:
Traceback (most recent call last):
File "C:/Users/*****/PycharmProjects/untitled/first.py", line 19, in
<module>
FTHG = int(row[4])
ValueError: invalid literal for int() with base 10: 'FTHG'
为了对CSV文件中的足球数据进行一些分析, 代表得分目标的值需要存储为整数。 由于CSV文件中的所有数据都是字符串数据,因此我需要转换。 我使用的语法确实可以在循环之外使用。
答案 0 :(得分:0)
尝试:
import csv
with open('Football_data.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
################ This is the header of each columns
next(readCSV) ## This line reads it so it is not read
################ when iterating in the for loop
# ... #