将数组拆分为字符串并显示Python

时间:2014-10-05 16:46:53

标签: python arrays string

所以,我正在尝试从bookfile.txt获取信息并显示信息。到目前为止,我已经打开了文件。我无法将每一行作为arrayBooks的一个对象。

我想拥有arrayBooks [0] =“Animal Farm,1945,152,George Orwell”

然后,我将使用book1 = arrayBooks [0] .split(',')将其拆分为其他信息,例如:

  book1[0]="Animal Farm"
  book1[1]=1945
  book1[2]=152
  book1[3]="George Orwell"

所以,当我想找到最短的书时,我可以将book1 [2]与book2 [2]和book3 [2]进行比较。

我的主要问题是获取数组中的信息并可用。我尝试的任何东西似乎都不起作用,并在displayAll()函数中给出错误。

我正在使用displayAll()作为控件,因为我觉得如果我可以显示信息,我会使用它。


bookfile.txt:

Animal Farm,1945,152,George Orwell

杀死一只知更鸟,1960,324,Harper Lee

Pride and Prejudice,1813,279,Jane Austen和Anna Quindlen


 def main():
      print("Welcome!")
      displayBookMenu()
      populateBooks()
      displayAll(populateBooks())

 def displayBookMenu:
      print("\n1: Display All Books")
      print("2: Display Shortest Book")
      print("3: Display Longest Book")
      print("3: Display Oldest Book")
      print("4: Display Newest Book")
      print("0: End")
      choice = int(input("Choice: "))

      if choice == 1:
           displayAll()
      elif choice == 2:
           displayShortest()
      elif choice == 3: 
            displayLongest()
      elif choice == 4: 
           displayOldest()
      elif choice == 5:
           displayNewest()
      elif choice == 0:
           exit()
      else:
           print("Invalid Input")

 def populateBooks():
      fp = open("bookfile.txt", "r")
      return fp.readlines()

 def displayAll(arrayBooks):
      print ("\nAll Books: \n")
      #THIS IS WHERE I GET ERROR vvv
      print arrayBooks[0]


 def displayShortest():

 def displayLongest():

 def displayOldest():

 def displayNewest():


 main()

2 个答案:

答案 0 :(得分:0)

试试这个:

lines = [line.strip().split(',') for line in open("books.txt")]

使用列表混合,您可以将文件读入名为lines的列表,并将文件中的每一行转换为列表列表。

这是我运行时得到的结果:

`lines = [line.strip().split(',') for line in open("books.txt")]

print lines
print lines[2]
print lines [2][1]`
enter code here

[['Animal Farm', '1945', '152', 'George Orwell'], ['To Kill A Mockingbird', '1960', '324', 'Harper Lee'], ['Pride and Prejudice', '1813', '279', 'Jane Austen and Anna Quindlen']]
['Pride and Prejudice', '1813', '279', 'Jane Austen and Anna Quindlen']
1813

现在有很多编辑。这里。确保仔细查看它们:

`def displayBookMenu():
      print("\n1: Display All Books")
      print("2: Display Shortest Book")
      print("3: Display Longest Book")
      print("3: Display Oldest Book")
      print("4: Display Newest Book")
      print("0: End")
      choice = int(input("Choice: "))

      if choice == 1:
           displayAll(populateBooks())
      elif choice == 2:
           displayShortest()
      elif choice == 3: 
            displayLongest()
      elif choice == 4: 
           displayOldest()
      elif choice == 5:
           displayNewest()
      elif choice == 0:
           exit()
      else:
           print("Invalid Input")

def populateBooks():
      lines = [line.strip().split(',') for line in open("books.txt")]
      return lines

def displayAll(arrayBooks):
      print ("\nAll Books: \n")
      #THIS IS WHERE I GET ERROR vvv
      for books in arrayBooks:
            for each_entry in books:
                  print each_entry,
            print

def displayShortest():
      pass
def displayLongest():
      pass
def displayOldest():
      pass
def displayNewest():
      pass

def main():
      print("Welcome!")
      displayBookMenu()
      populateBooks()
main()

删除displayBooks(populateBooks()),而不是在if choice ==1语句中。

答案 1 :(得分:-1)

首先,您有错误

def displayBookMenu: #needs ()

并且您无法读取ArrayBooks,因为它不是全局(或非本地)变量。我认为你有可变范围的问题。检查一下:Python variable scope error