繁体   English   中英

从python中读取文件中的数据,但始终从文件中获取特定数据

[英]Reading data from a file in python, but taking specific data from the file throughout

嗨,我正在尝试从.dat文件读取数据,该文件具有以下信息:

1
Carmella Henderson
24.52
13.5
21.76
2
Christal Piper
14.98
11.01
21.75
3
Erma Park
12.11
13.51
18.18
4
Dorita Griffin
20.05
10.39
21.35
5 
Marlon Holmes
18.86
13.02
13.36

根据这些数据,我需要人员编号,姓名和第一个数字,如下所示:

1 #person number
Marlon Holmes  #Name
18.86 # First number
13.02
13.36

但是目前我的代码是从文件读取数据而不是这些特定部分,它只是打印文件

这是我当前针对此特定部分的代码:

def Cucumber_Scoreboard():
    with open('veggies_2015.dat', 'r') as f:
        count = 0
        for line in f:
            count **= 1
            if count % 2 == 0:
                print (line)

我不确定它出错的地方,我试图将文件中的数据放入列表并从那里尝试但没有成功,任何帮助将不胜感激

整个文件代码(如果需要):

def menu():
    exit = False


    while not exit:
        print("To enter new competitior data, type new")
        print("To view the competition score boards, type Scoreboard")
        print("To view the Best Overall Growers Scoreboard, type Podium")
        print("To review this years and previous data, type Data review")
        print("Type quit to exit the program")

        choice = raw_input("Which option would you like?")

        if choice == 'new':
            new_competitor()
        elif choice == 'Scoreboard':
            scoreboard_menu()
        elif choice == 'Podium':
            podium_place()
        elif choice == 'Data review':
            data_review()
        elif choice == 'quit':
            print("Goodbye")
            raise SystemExit

"""Entering new competitor data: record competitor's name and vegtables lengths"""

def competitor_data():
    global competitor_num
    l = []

    print("How many competitors would you like to enter?")

    competitors = raw_input("Number of competitors:")

    num_competitors = int(competitors)

    for i in range(num_competitors):

        name = raw_input("Enter competitor name:")
        Cucumber = raw_input("Enter length of Cucumber:")
        Carrot = raw_input("Enter length of Carrot:")
        Runner_Beans = raw_input("Enter length of Runner Beans:")

        l.append(competitor_num)
        l.append(name)
        l.append(Cucumber)
        l.append(Carrot)
        l.append(Runner_Beans)

        competitor_num += 1

    return (l)



def new_competitor():
    with open('veggies_2016.txt', 'a') as f:
        for item in competitor_data():
            f.write("%s\n" %(item))



def scoreboard_menu():
    exit = False

    print("Which vegetable would you like the scoreboard for?")

    vegetable = raw_input("Please type either Cucumber, Carrot or Runner Beans:")

    if vegetable == "Cucumber":
        Cucumber_Scoreboard()
    elif vegetable == "Carrot":
        Carrot_Scoreboard()
    elif vegetable == "Runner Beans":
        Runner_Beans_Scoreboard()

def Cucumber_Scoreboard():
    with open('veggies_2015.dat', 'r') as f:
        count = 0
        for line in f:
            count **= 1
            if count % 2 == 0:
                print (line)

您可以让程序逐行读取整个文件,获取所有信息。 然后,因为数据采用已知格式(例如,位置,名称...),请使用file.readline()跳过不需要的行,这会将您移至下一行。

创建一个函数一次读取一个“记录”(5行),然后重复调用它:

def read_data(in_file):
    rec = {}
    rec["num"] = in_file.next().strip()
    rec["name"] = in_file.next().strip()
    rec["cucumber"] = float(in_file.next().strip())

    # skip 2 lines
    in_file.next()
    in_file.next()

    return rec

编辑:改进了代码+添加了用法示例

read_data()函数从文件中读取一个5行记录,并将其数据作为字典返回。 使用此功能的一个示例:

def Cucumber_Scoreboard():
    with open('veggies_2015.dat', 'r') as in_file:
        data = []

        try:
            while True:
                rec = read_data(in_file)
                data.append(rec)

        except StopIteration:
            pass

    data_sorted = sorted(data, key = lambda x: x["cucumber"])

    return data_sorted

cucumber = Cucumber_Scoreboard()

from pprint import pprint
pprint(cucumber)

这并不是最优雅的方式,但是如果你逐行进行,你需要一个额外的计数器,这会导致在重置计数器之前没有任何事情发生在一定量的“剩余”线上。 请注意, excess_count只需要递增一次,因为您希望最后的else重置两个计数器,这不会导致打印的内容但仍会导致跳过的行。

def Cucumber_Scoreboard():
    with open('name_example.txt', 'r') as f:
        count = 0
        excess_count = 0
        for line in f:
            if count < 3:
                print (line)
                count += 1
            elif count == 3 and excess_count < 1:
                excess_count += 1
            else:
                count = 0
                excess_count = 0

编辑:根据您的评论,我扩展了这个答案。 真的,你提出的问题应该被提出作为另一个问题,因为它与你的主要问题脱节了。 正如jDo所指出的,这不是理想的代码,因为如果有空行或缺少数据导致行人为跳过,它将立即失败。 此外,新代码填写在我的初始答案周围。 使用它只是为了说明在循环中重置计数器和列表,它对于严肃的事情是不稳定的。

from operator import itemgetter

def Cucumber_Scoreboard():
    with open('name_example.txt', 'r') as f:
        count = 0
        excess_count = 0
        complete_person_list = []
        sublist = []

        for line in f:
            if count < 3:
                print (line)
                sublist.append(line.replace('\n', ''))
                count += 1
            elif count == 3 and excess_count < 1:
                excess_count += 1
            else:
                count = 0
                excess_count = 0
                complete_person_list.append(sublist)
                sublist = []

        sorted_list = sorted(complete_person_list, key=itemgetter(2), reverse = True)
    return sorted_list

a = Cucumber_Scoreboard()

最近有人问如何保存他/她的游戏的玩家分数,我最终编写了一个快速演示。 我没有发布它,因为它会有点有帮助。 目前的形式,它并不完全适合你的游戏,但也许你可以用它来获取灵感。 无论你做什么,不依赖于行号,模数和计数都会让你头疼不已(如果有人添加了空/额外行怎么办?)。

所有数据类型都有优点和缺点。 如果我们将您当前的数据格式(没有键或类别/列标签的换行符分隔值)与json进行比较,那么您的空间使用率实际上更高效。 您没有任何重复。 在像json和python词典这样的键/值对格式中,你经常反复重复这些键。 这使得它成为一种人类可读的格式,它使得顺序无关紧要,这意味着整个事物可以写在一行上。 但是,不用说为每个玩家重复所有按键是没有效率的。 如果有100,000名玩家并且他们都有名字,姓氏,高分和last_score,那么你将重复这4个单词100.000次。 这是实际数据库成为数据存储的理想选择。 不过就您而言,我认为json就足够了。

import json
import pprint

def scores_load(scores_json_file):
    """ you hand this function a filename and it returns a dictionary of scores """
    with open(scores_json_file, "r") as scores_json:
        return json.loads(scores_json.read())

def scores_save(scores_dict, scores_json_file):
    """ you hand this function a dictionary and a filename to save the scores """
    with open(scores_json_file, "w") as scores_json:
        scores_json.write(json.dumps(scores_dict)) 

# main dictionary of dictionaries - empty at first
scores_dict = {}

# a single player stat dictionary. 
# add/remove keys and values at will
scores_dict["player1"] = {
    "firstname" : "whatever",
    "lastname" : "whateverson",
    "last_score" : 3,
    "highscore" : 12,
}

# another player stat dictionary
scores_dict["player2"] = {
    "firstname" : "something",
    "lastname" : "somethington",
    "last_score" : 5,
    "highscore" : 15,
}


# here, we save the main dictionary containing stats 
# for both players in a json file called scores.json
scores_save(scores_dict, "scores.json")

# here, we load them again and turn them into a 
# dictionary that we can easily read and write to
scores_dict = scores_load("scores.json")

# add a third player 
scores_dict["player3"] = {
    "firstname" : "person",
    "lastname" : "personton",
    "last_score" : 2,
    "highscore" : 3,
}

# save the whole thing again
scores_save(scores_dict, "scores.json")

# print player2's highscore
print scores_dict["player2"]["highscore"]

# we can also invent a new category (key/value pair) on the fly if we want to
# it doesn't have to exist for every player
scores_dict["player2"]["disqualified"] = True

# print the scores dictionary in a pretty/easily read format.
# this isn't necessary but just looks nicer
pprint.pprint(scores_dict)

"""
The contents of the scores.json pretty-printed in my terminal:

$ cat scores.json | json_pp

{
   "player3" : {
      "firstname" : "person",
      "last_score" : 2,
      "lastname" : "personton",
      "highscore" : 3
   },
   "player2" : {
      "highscore" : 15,
      "lastname" : "somethington",
      "last_score" : 5,
      "firstname" : "something"
   },
   "player1" : {
      "firstname" : "whatever",
      "last_score" : 3,
      "lastname" : "whateverson",
      "highscore" : 12
   }
}


"""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM