简体   繁体   English

如何在Python中将文本从文件转换为列表?

[英]How to convert text from a file into a list in Python?

I am currently making a program for my school work that asks the user 3 sets of 10 randomly generated questions, and then stores the students results in one of three text files, depending on which class they are in. The program should then be able to load all the data from the text file, so that when the next student takes the quiz, their results are appended to the rest. 我目前正在为学校作业编写一个程序,该程序向用户询问3组10个随机生成的问题,然后将学生的结果存储在三个文本文件之一中,具体取决于他们所在的班级。然后,该程序应该能够从文本文件中加载所有数据,以便当下一个学生参加测验时,其结果将附加到其余部分。 It is stored in a list called "class1", "class2" or "class3", depending on what class the user is in. The data can then be sorted in different ways. 它根据用户所在的类存储在名为“ class1”,“ class2”或“ class3”的列表中。然后可以用不同的方式对数据进行排序。 In this post, we will assume the user is in Class 1. 在这篇文章中,我们将假设用户属于第1类。

The program tracks the users name, highest score, average score and their three attempts at the quiz and stores of it into the list "class1". 该程序跟踪用户的姓名,最高分,平均分及其在测验中的三次尝试,并将其存储在列表“ class1”中。 Below is the list "class1". 下面是列表“ class1”。

[('Albert', 6, 2, [6, 0, 0]), ('Bob', 6, 2.33, [6, 1, 0]), ('Cameron', 5, 4, [5, 2, 5])].

If the program has never run before, then the program will simply create a new text file called "Class 1 Data.txt". 如果该程序从未运行过,则该程序将简单地创建一个名为“ Class 1 Data.txt”的新文本文件。 If this file already exists, however, then this following section of code runs: 但是,如果此文件已经存在,则运行以下代码部分:

f = open("C:/Users/Custom/Desktop/Class " + str(classNumber) + " Data.txt", "r")
lines = f.readlines()
oldData = lines[3]

The variable "oldData" is now just a long string containing " [('Albert', 6, 2, [6, 0, 0])... ". 变量“ oldData”现在只是一个包含“ [['Albert',6,2,[6,0,0])...”的长字符串。 Now lets assume the program is turned off and then loaded by another student called Zara, who then takes the quiz. 现在,假设该程序已关闭,然后由另一名叫Zara的学生加载,然后参加测验。 Since "oldData" is just a string and not a list, when I try to append it back to the list "class1", this is what happens: 由于“ oldData”只是一个字符串而不是一个列表,因此当我尝试将其附加回列表“ class1”时,将发生以下情况:

[('Zara', 9, 6, [3, 9, 6]), "[('Albert', 6, 2, [6, 0, 0]), ('Bob', 6, 2.33, [6, 1, 0]), ('Cameron', 5, 4, [5, 2, 5])]\n"]

How would I go about trying to load text from a file and then storing it as a list? 我将如何尝试从文件中加载文本,然后将其存储为列表? I have tried using various methods, however none of them worked. 我尝试使用各种方法,但是没有一个起作用。 Any help would be greatly appreciated. 任何帮助将不胜感激。

It seems like you've just copied and pasted the textual representation (within Python) of your data into a text file, and now you want to take that text and convert it (from text) back into a Python data structure. 似乎您刚刚将数据的文本表示形式(在Python中)复制并粘贴到了文本文件中,现在您想要获取该文本并将其(从文本中)转换回Python数据结构中。 This is an overly complicated way of doing that, I think. 我认为这是一种过于复杂的方式。 Parsers and the grammar involved in parsing are fairly complicated, and it would be very difficult for you to reproduce that. 解析器和解析所涉及的语法相当复杂,您很难重现该解析器。 Plus I just don't think it's a valuable use of your time. 另外,我只是认为这不是您宝贵的时间。

That leaves you with two choices: 这给您两个选择:

  1. If you actually want to serialize your python types to disk, I would strongly recommend trying to use the pickle library. 如果您实际上要将Python类型序列化为磁盘,则强烈建议尝试使用pickle库。 This is a little complicated, but once you get the hang of it you should just be able to pickle and pickle objects of totally arbitrary complexity, rather than trying to parse them from plain old text. 这有点复杂,但是一旦掌握了窍门,您就应该能够腌制和腌制完全任意复杂的对象,而不是尝试从普通的旧文本中解析它们。

  2. If you don't do that, you should find a better, more predictable, more easily-parsed way of saving the data to text. 如果您不这样做,则应该找到一种更好的,更可预测的,更容易解析的方式将数据保存为文本。

  3. Within your program itself, you should create classes to more easily encapsulate your data - you have a list of tuples of strings and integers and lists of integers. 在程序本身中,您应该创建类以更轻松地封装数据-您有一个字符串和整数元组列表以及一个整数列表。 It's a little much to walk through absent any object-oriented structure. 缺少任何面向对象的结构都需要花很多时间。

For example, if you were to use a different textual representation that's not tied to the way python types look: 例如,如果要使用不依赖于python类型外观的其他文本表示形式:

name:Zara highscore:9 averagescore:6 attempt1:3 attempt2:9 attempt3:6
name:Albert highscore:6 averagescore:2 attempt1:6 attempt2:0 attempt3:0

Or if you were to use XML, you could save your document something like this: 或者,如果您要使用XML,则可以将文档保存如下:

<users>
    <user name="yrName" highscore="yrScore" averagescore="yrAverage" 
          attempt1="1" attempt2="2" attempt3="3">       
    </user>
    <user>
        ...
</users>

And you could use xml.etree.ElementTree to walk through the nodes and pick out each piece of information you needed. 您可以使用xml.etree.ElementTree遍历节点并挑选出所需的每条信息。

I guess the biggest question about this, though, isn't why you're storing data the way you are but why you're storing a lot of it in the first place. 我想最大的问题关于这一点,虽然,是不是你为什么存储数据你的方式 ,但为什么你存储大量的它摆在首位。 40% of your data - all high scores and average scores - has no reason at all to be stored. 您所有数据的40%(所有高分和平均分)完全没有理由要存储。 These figures are trivially calculated if you have access to the three attempts, and create so, so much more work for you than just using (one + two + three) / 3 or min([one, two, three]). 如果可以访问这三个尝试并进行创建,那么这些数字将被微不足道地计算,从而为您提供的工作量比仅使用(一个+两个+三个)/ 3或min([一个,两个,三个])要多得多。

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

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