CSV 파일에 입력 된 정보 집합 (http://www.databasebasketball.com/players/playerlist.htm)을 기반으로 코드를 작성하고 있습니다.파이썬에서 csv 파일의 문자열을 정수로 변환하는 방법은 무엇입니까?
각 선수의 BMI를 결정하는 코드를 만들고 싶습니다. 그런 다음 BMI가 30 이상이면 비만이라고 생각합니다.
그러나 플레이어의 높이를 피트 단위로 높이로 줄여야하며 원본 CSV 파일을 변경하지 않고도 플레이어의 키 높이를 높이는 방법을 모르겠습니다.
import csv
def read_csv(filename):
"""
Reads a Comma Separated Value file,
returns a list of rows; each row is a dictionary of columns.
"""
with open(filename, encoding="utf_8_sig") as file:
reader = csv.DictReader(file)
rows = list(reader)
return rows
# Try out the function
players = read_csv("players.csv")
# Print information on the first player, to demonstrate how
# to get to the data
from pprint import pprint
pprint(players[0])
print(players[0]["lastname"])
print(players[0]["weight"])
total_h_inches = print(players[0]["h_feet"*12])
을하지만
Traceback (most recent call last):
File "C:\Python34\hw5.py", line 24, in <module>
h_feet = print(players[0]["h_feet"*12])
KeyError: 'h_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feet'
은 내가 아주 멀리 최종 결과에서임을 알게 오류를 반환합니다,하지만 난 도움이 단계를 통해 얻는 느낌 :
지금까지 내가 가진 많이.
오류는 자체 설명입니다 : 'h_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feeth_feet'와 같은 키가 없습니다. 인치로 변환 할 플레이어 [0] [ 'h_feet'] * 12를 찾고 있습니다. – kvivek
사람들이 답변 한 후에는 질문을 변경하지 마십시오. 이미 답변과 관련이 없으므로 사용자에게 혼동을 줄 수 있습니다. 대신 새로운 질문을하십시오 (새로운 문제를 조사하기 위해 조금만 시간을 할애 해보십시오). – Stuart