簡體   English   中英

從文件中讀取行並將其存儲在列表中

[英]Reading Lines from File and Storing it in List

我正在從文本文件中讀取行,我希望列表中的每一行都成為數組中不同元素的一部分。

with open("diceRoll.txt","r") as x_file:
    contents = x_file.readlines()

oneScore = contents[count-1]
oneScore = oneScore.split(" ")
print(oneScore)
n = oneScore[0] + " " + oneScore[

當我運行該程序一次時,我得到以下響應:

['i', '68']

當我運行該程序兩次時,我得到以下響應:

['i', '68j', '22']

如何更改我的代碼,以便當我運行我的程序兩次時,我會收到68, j, , 而不是'68j'

lines_list=[line.split(“ “) for line in open(“diceRoll.txt”,”r”)]
last_line=lines_list[-1]

應該讓您將文件的行作為列表列表,並為您提供文件的最后一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM