我似乎無法弄清楚如何使用文本文件中給出的值並將它們導入到python中以創建列表。我在這裏想要完成的是創建一個遊戲板,然後將數字作爲樣本集。我必須使用Quickdraw來實現這一點 - 我知道如何獲得Quickdraw上的數字,但我似乎無法從文本文件中導入數字。先前的分配涉及讓用戶輸入值或使用I/O重定向,這有點不同。任何人都可以幫助我嗎?這裏導入文本文件以在Python 3.x中創建列表?
0
A
回答
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
如果我們知道輸入和輸出要求,它可能會更準確。
1
兩個步驟:
open文件
讀取線
此頁面可以幫助你:http://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
相關問題
- 1. Python 3.X寫入文本文件不會創建新行
- 2. 將大文本文件導入Python並創建列表清單
- 3. 從Python中的文本文件導入3-D列表變量
- 4. python-3.x酸洗創建空文件
- 5. 將csv文件導入python並在python中創建表格
- 6. Python導入文本文件
- 7. 在Python中創建文本文件
- 8. 如何在Python中創建列表列表以存儲文本列表?
- 9. 如何在Python中將文本列表寫入文本文件
- 10. 從文本文件python中創建嵌套列表的函數
- 11. 如何導入文件文本列表
- 12. 從文本文件讀入python列表
- 13. 導入.dat文件在Python 3
- 14. Python:如果列表中不存在文件,則創建文件
- 15. 將Python文本文件導入.csv
- 16. 將文本導入Python中的文件
- 17. 在Python中導入.txt文件後,在列表[x]中爲列表[x]添加特定項目
- 18. Python的導入文本文件列表重述
- 19. Python:從文本文件導入到子列表
- 20. 從文本文件創建n * n矩陣包含Python列表中的列表
- 21. 在python中將文本文件導入到2d列表中添加單引號
- 22. Python中創建的文本文件
- 23. 導入CSV文件到列表python
- 24. 在Python中創建向前兼容的OpenGL 3.x上下文
- 25. 刪除導入的文本文件(Python)
- 26. 從文本文件導入並在matlab中創建一個單元格陣列
- 27. 用Python在文本文件中創建彩虹表
- 28. 在Python中導入文件
- 29. Laravel 3創建文本(CSV)文件
- 30. 導入文本文件並對其進行操作以創建新文件
看看內建[開放](http://docs.python.org/3.2/library/functions.html#open)初學者 –
向我們展示您試過的內容,以便我們指出您的位置,重新出錯 – inspectorG4dget
你的問題很難理解。你究竟試過了什麼,以及你在執行中遇到了哪些問題? – user4815162342