【问题标题】:Reading Lines from File and Storing it in List从文件中读取行并将其存储在列表中
【发布时间】:2020-04-29 00:46:34
【问题描述】:

我正在从文本文件中读取行,我希望列表中的每一行都属于数组中不同元素的一部分。

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'

【问题讨论】:

    标签: python arrays list file


    【解决方案1】:
    lines_list=[line.split(“ “) for line in open(“diceRoll.txt”,”r”)]
    last_line=lines_list[-1]
    

    应该将文件的行作为列表列表提供给您,并提供文件的最后一行。

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 2021-06-03
      • 2020-02-09
      • 2017-11-05
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多