import csv
fields = [
"RowNum",
"PlayerNum",
"Name",
"Position",
"Age",
"ThrowHand",
"BatHand",
"Height",
"Weight",
"Birthdate",
"RookieYear"
]
with open("astroRoster.txt") as astroFiles:
astroRoster = csv.DictReader(astroFiles, fields)
print("\t Player names and jersey numbers:")
for element in astroRoster:
print(f'{element["Name"]}: {element["PlayerNum"]}')
with open("astroRoster.txt") as astroFiles:
astroRoster = csv.DictReader(astroFiles, fields)
print("\t Tallest player and height:")
for element in astroRoster:
print(f'{element["Name"][17]}: {element["Height"][17]}') #this is where i have no idea what i'm doing
我正在尝试从我的.txt文件中打印出最高的播放器及其高度,并附有该文件的照片。但是我不确定如何获取特定元素。我希望能够打印出他们的体重,年龄和其他特定事物的平均值,但是我不知道/不知道如何。我是一个超级初学者,所以很抱歉,如果这是一个愚蠢的问题,我就是想不通:(
答案 0 :(得分:0)
对于表格数据而言,熊猫是一个了不起的工具! 打印数据,您可以使用下面的内容(未经测试)。您将需要对其进行修改以保存到文本文件。
import pandas as pd
df = pd.read_csv("yourcsv",names=["RowNum","PlayerNum","Name","Position","Age","ThrowHand","BatHand","Height","Weight","Birthdate","RookieYear"])
for row in df.rows:
print row['Name'], row['PlayerNum'], row['Height']