1

我是 python 和一般编程的新手,所以如果我没有正确解释或使用正确的术语,请多多包涵。同样,如果你给我一个非常技术性的回答,我可能不会理解你。我正在尽力而为,但这对我来说是新的并且非常困难!

我正在尝试学习如何使用 python,所以我开始创建一个小程序来跟踪与徒步旅行相关的各种事情,比如过去的徒步旅行、里程等。我找到了一个名为 prettytable 的库,我安装并导入了该格式的数据输入到一个表中。我在输入数据时得到了这个处理,但不是来自保存的文件。我希望能够保存输入到程序中的数据以备后用。

这是我正在使用的创建远足列表的模块。每次徒步旅行都有目的地、徒步里程、徒步天数和伙伴。程序的这一部分按预期工作。输入信息后,输入的数据将存储为一个名为 stored_words 的列表,然后将该列表作为一行添加到 prettytable 中。完成徒步旅行后,表格会打印出来。

def Hikes():

    import prettytable
    import csv

    answer = "yes"
    Index = 0


    mytable = prettytable.PrettyTable()

    x = prettytable.PrettyTable(["Destination", "Miles Hiked", "Days Hiked", "Partner"])
    x.align["Destination"] = "l" # Left align destination
    x.padding_width = 1 # One space between column edges and contents (default)

    while answer == "yes":
        words = ["destination", "miles hiked", "days hiked", "partner"]
        stored_words = []
        HikeList = []
        hikesave = open("hikes.txt" , "a")
        for word in words:
            answer = input("Enter the %s:" % word)
            stored_words.append(answer)
            print(stored_words)

        HikeList.append(stored_words)
        hikesave.write('\n')
        hikesave.write(str(stored_words))

        x.add_row(stored_words)

        hikesave.close()

        answer = input("Enter another hike? yes or no: ")


    print(x)

    return x

这是我为加载文件而创建的模块:

def LoadFile():

    import prettytable
    Flag = False
    FileToLoad = str



    mytable = prettytable.PrettyTable()

    x = prettytable.PrettyTable(["Destination", "Miles Hiked", "Days Hiked", "Partner"])
    x.align["Destination"] = "l" # Left align destination
    x.padding_width = 1 # One space between column edges and contents (default)

    print("-----------------------------------")
    print("     A = Hikes")
    print("     B = Miles List")
    print("     B = Destinations List")
    print("-----------------------------------")
    print()

    FileToLoad = input("Which file would you like to open?")

    while Flag == False: #Loop will keep running until true
        while FileToLoad == "A" or FileToLoad == "B" or FileToLoad == "C":
            if FileToLoad == "A

                with open("hikes.txt", "r") as f:   
                    lines = f.readline()
                    print(lines)                    

                x.add_row(lines)

##                f = open("hikes.txt", "r")
##                for line in f:
##                    print(line)
##                    x.add_row(line)


                print(x)

                Flag = True

我只是不知道如何在 LoadFile 模块中加载文件(hikes.txt)并将该文件中的数据逐行添加到漂亮表中。当我尝试这样做时,我的输出中出现此错误:

-----------------------------------
     A = Hikes
     B = Miles List
     C = Destinations List
-----------------------------------

Which file would you like to open?A
['High Country Pathway, MI', '82', '4', 'solo']

Traceback (most recent call last):
  File "D:\Program Files\PROGRAMMING\Python\11-3-13.py", line 260, in <module>
    main()
  File "D:\Program Files\PROGRAMMING\Python\11-3-13.py", line 66, in main
    c = LoadFile()
  File "D:\Program Files\PROGRAMMING\Python\11-3-13.py", line 216, in LoadFile
    x.add_row(lines)
  File "D:\Program Files\PROGRAMMING\Python\lib\site-packages\prettytable.py", line 818, in add_row
    raise Exception("Row has incorrect number of values, (actual) %d!=%d (expected)" %(len(row),len(self._field_names)))
**Exception: Row has incorrect number of values, (actual) 48!=4 (expected)**

我不明白为什么会收到上述错误。

这个:

['High Country Pathway, MI', '82', '4', 'solo']

应该以漂亮的表格添加到行中...与上一个模块中使用的格式相同。

任何帮助将不胜感激。我找不到有关收到的错误消息的任何信息,并且一直在阅读文件输入/输出,但无法弄清楚。我知道我的代码可能有很多问题,但请记住,我对 python 非常陌生,只是想让事情正常工作,所以请不要对我太苛刻。

**编辑

Stevha,我尝试了您提供的代码,但我仍然收到相同的错误,除了现在是 7!=4:

回溯(最后一次调用):文件“D:\Program Files\PROGRAMMING\Python\11-3-13.py”,第 265 行,在 main() 文件“D:\Program Files\PROGRAMMING\Python\11- 3-13.py",第 66 行,在 main c = LoadFile() 文件中 "D:\Program Files\PROGRAMMING\Python\11-3-13.py",第 219 行,在 LoadFile x.add_row(row) 文件中"D:\Program Files\PROGRAMMING\Python\lib\site-packages\prettytable.py", line 818, in add_row raise Exception("Row has wrong number of values, (actual) %d!=%d (expected) " %(len(row),len(self._field_names))) 异常:行的值数不正确,(实际)7!=4(预期)

我之前看过 pickle 和 JSON,并没有真正理解我需要做什么。我认为文本文件会起作用。它适用于打印信息,没问题,但到目前为止还不能插入漂亮的表格中。我也在研究 csv 导入,但试图让它首先与文本文件一起使用。

也感谢您的其他建议。当我让事情正常工作并进行输入验证等时,我打算稍后通过代码,但现在我只是四处寻找,试图让事情正常工作。我正在探索我能用 python 做什么和不能做什么,试图看看事情是如何运作的。我知道那里有一些未使用的代码,例如 HikeList 列表。我这样做是为了尝试以不同的方式在文本文件中保存数据,以期克服该错误。

当我输入保存到 hikes.txt 文件中的数据时,我的程序如下所示:

    Choose from the list below: 

     A = Enter Miles Hiked
     B = Enter hiking destination
     C = Load File                        
     E = Exit program
--------------------------------------------------

Enter your selection:  b
Enter the destination:Big Bend, TX
['Big Bend, TX']
Enter the miles hiked:45
['Big Bend, TX', '45']
Enter the days hiked:5
['Big Bend, TX', '45', '5']
Enter the partner:Dan
['Big Bend, TX', '45', '5', 'Dan']
Enter another hike? yes or no: no
+--------------+-------------+------------+---------+
| Destination  | Miles Hiked | Days Hiked | Partner |
+--------------+-------------+------------+---------+
| Big Bend, TX |      45     |     5      |   Dan   |
+--------------+-------------+------------+---------+

打印语句暂时存在,因此我可以看到输入到文本文件中的内容。在向 hikes.txt 文件添加新的远足后,它看起来像这样:

['High Country Pathway, MI', '82', '4', 'solo']
['Wemincuhe Wilderness, CO', '55', '6', 'Lisa']
['Big Bend, TX', '45', '5', 'Dan']

文本文件中的每一行都包含一个列表格式的漂亮表中所有 4 列的值。错误告诉我没有 4 个值,但不是我有一个列表,每个列表中包含 4 个值吗?如果它认为它是一个字符串而不是一个列表,那么我如何告诉 python 我的 hikes.txt 文件中的每一行都是一个列表?

4

1 回答 1

0

I think you will want some code similar to this:

with open("hikes.txt", "r") as f:   
    for line in f:
        row = line.split()
        x.add_row(row)

You can take the "open file" object f and use it for a for loop. When you do this, the loop variable (in this example row) will be set to one line from the file and the loop body run.

By the way, when you prompt the user for input, you might want to do something like this:

choice = input("Which file would you like to open?")
choice = choice.strip() # remove leading and trailing white space
choice = choice.lower() # convert to lower-case

Then you can compare choice to 'a' and 'b' and so on, and it will be a bit forgiving (user won't need to upper-case, and if the user hits the space bar by mistake it will still work).

You can do the above all on one line:

choice = input("Which file would you like to open?").strip().lower()

EDIT: I don't really understand your code for saving hike data. It looks like you are writing all the hike data at once and I don't think the way you are doing it will work.

I suggest you either write a very simple format, one line at a time, or else save all the data in a list and write the list using a library function. The best choices would be Python's native pickle or JSON using the json module.

Assuming you want to write a simple format, CSV would be a good choice. If you Google search for "Python CSV tutorial" you will find a lot of help, and there is a lot of stuff here on StackOverflow about it too.

EDIT: Also, it is clear you are very new to Python. I would encourage you to work through an introduction to Python, such as "Learn Python the Hard Way", or read a good Python book.

It's not 100% required, but it is a very good idea to learn the idioms of the Python community, and follow them. For example, variable names should use lower-case letters and underscores; initial-caps should be reserved for class names. This is written up in a standard called "PEP 8". If you look at how StackOverflow has syntax-highlighted your code, you will see that HikeList is shown differently than other variables; this is because it is being flagged as a class name since it starts with a capital letter.

Also, my advice is to break up your program into small pieces and test each piece. Have you tested that your file-creation code does what you wanted it to do? Have you looked at the output file hikes.txt that you get when you are done?

Also, here is what that error message means. You created a PrettyTable class instance, and set it up for a 4-column table. So it expects each input row to be a list with 4 items in the list. But you passed a string. Python will pretend a string is a list of characters, so your PrettyTable instance saw a list of 48 things (each thing being one character). My code example uses the .split() method function to split a string on whitespace, but you could use CSV with some other separator and it would probably be better.

于 2013-11-12T22:56:42.963 回答