1

我目前正在为学校做一个涉及制作图形编辑器的项目。我处于必须能够保存和重新打开文件的位置。我可以打开文件,但我必须遍历它并重新绘制我保存的所有内容。但是我不确定是否要真正遍历文件,因为当打印我打开的文件时,我会得到一个巨大的列表,其中包含我的所有列表,如下所示:

["['Rectangle', 5.168961201501877, 8.210262828535669, 7.6720901126408005, 6.795994993742178, 'red']['Line', 5.782227784730914, 5.269086357947434, 8.69837296620776, 4.993742177722153, 'red']['Circle', 2.6491232154288933, -0.8552572601656006, 6.687547623119292, 3.1831671475247982, 'red']"]

我是使用这个网站的新手,所以请多多包涵。

def open_file(self,cmd):
    filename=input("What is the name of the file? ")
    File= open(filename,'r')
    file= File.readlines()
    print(file)

我以前使用以下方法保存了文件:

file.write(str(l)) 其中 l 是我创建的值列表的名称

我尝试使用 split() 我尝试使用 for 循环将字符串中的数据保存到一个列表中,我已经在网上搜索了几个小时以找到某种解释,但我找不到任何解释。

4

2 回答 2

1

您提供的实际上是一个列表,其中一个项目由一个长字符串组成。你能提供你用来生成这个的代码吗?

如果它实际上是列表中的列表,则可以在另一个 for 循环中使用 for 循环来访问每个列表中的每个项目。

假设您的列表是对象 l。

l[0] = ['矩形',5.168961201501877,8.210262828535669,7.6720901126408005,6.795994993742178,'红色']

和 l[0][0] = '矩形'

for i in l:
    for x in i:

将允许您遍历所有这些。

对于您提供的信息, readlines() 不一定有效,因为没有什么可以在文本中划定新行。您可以使用 for 循环将列表中的每个项目保存为一行,而不是将列表保存为转换后的字符串

for lne in l:
    f.write(lne)

这会将列表中的每个项目写入文件的新行(取决于您的 python 版本,您可能必须使用 f.write(lne+'\n') 添加新行)。然后,当您打开文件并使用 readlines() 时,它会将每一行作为一个项目附加到列表中。

于 2014-04-25T00:38:24.877 回答
0

You are apparently having problem with reading data you have created before.

Your task seem to require

1) creating some geometry in an editor

2) serialize all the geometry to a file

and later on (after the program is restarted and all old memory content is gone:

3) load geometries from the file

4) recreated the content (geometries) in your program

In step 2 you did something and you seem to be surprised by that. My proposal would be to use some other serialization option. Python offers many of them, e.g.

  • pickle - quick and easy, but is not interoperable with other than Python programs
  • JSON - easy, but might require some coding for serialization and loading your custom objects

Sample solution using JSON serialization could go like this:

import json

class Geometry():
    def __init__(self, geotype="Geometry", color="blank", numbers=[]):
        self.geotype = geotype 
        self.color = color
        self.numbers = numbers

    def to_list(self):
        return [self.geotype, self.color, self.numbers]

    def from_list(self, lst):
        print "lst", lst
        self.geotype, self.color, self.numbers = lst
        return self

    def __repr__(self):
        return "<{self.geotype}: {self.color}, {self.numbers}>".format(self=self)

def test_create_save_load_recreate():
    geoms = []
    rect = Geometry("Rectange", "red", [12.34, 45])
    geoms.append(rect)

    line = Geometry("Line", "blue", [12.33, 11.33, 55.22, 22,41])
    geoms.append(line)

    # now serialize
    fname = "geom.data"
    with open(fname, "w") as f:
        geoms_lst = [geo.to_list() for geo in geoms]
        json.dump(geoms_lst, f)
    # "geom.data are closed noe
    del f
    del geoms
    del rect
    del line
    # after a while
    with open(fname, "r") as f:
        data = json.load(f)
    geoms = [Geometry().from_list(itm) for itm in data]
    print geoms
于 2014-04-25T01:09:51.200 回答