2012-11-21 61 views
0

我似乎無法弄清楚如何使用文本文件中給出的值並將它們導入到python中以創建列表。我在這裏想要完成的是創建一個遊戲板,然後將數字作爲樣本集。我必須使用Quickdraw來實現這一點 - 我知道如何獲得Quickdraw上的數字,但我似乎無法從文本文件中導入數字。先前的分配涉及讓用戶輸入值或使用I/O重定向,這有點不同。任何人都可以幫助我嗎?這裏導入文本文件以在Python 3.x中創建列表?

+1

看看內建[開放](http://docs.python.org/3.2/library/functions.html#open)初學者 –

+1

向我們展示您試過的內容,以便我們指出您的位置,重新出錯 – inspectorG4dget

+1

你的問題很難理解。你究竟試過了什麼,以及你在執行中遇到了哪些問題? – user4815162342

回答

2

要看的內容你想要讀取並輸出到列表中的文件你想得到。

# assuming you have values each on separate line 
values = [] 
for line in open('path-to-the-file'): 
    values.append(line) 
    # might want to implement stripping newlines and such in here 
    # by using line.strip() or .rstrip() 

# or perhaps more than one value in a line, with some separator 
values = [] 
for line in open('path-to-the-file'): 
    # e.g. ':' as a separator 
    separator = ':' 
    line = line.split(separator) 
    for value in line: 
     values.append(value) 

# or all in one line with separators 
values = open('path-to-the-file').read().split(separator) 
# might want to use .strip() on this one too, before split method 

如果我們知道輸入和輸出要求,它可能會更準確。

相關問題