【问题标题】:I need to read 100000 lines of a txt file and add each line into a array [duplicate]我需要读取 100000 行 txt 文件并将每一行添加到数组中 [重复]
【发布时间】:2019-12-28 18:41:26
【问题描述】:

我有一个 TXT 文件,其中包含 100000 行 1-3 位长的短字符串。我需要一个循环 100000 次的 for 循环,在循环中我需要读取一行,然后将其添加到数组中,最后向下移动到下一行。 txt 文件名为 Rand_Numbers,我已将其打开为...

C_txt = open("Rand_Numbers", "r")

我真的不知道如何在 python 中做很多事情,因为我对编程场景还很陌生。任何帮助深表感谢。

【问题讨论】:

  • 我觉得这个question和你的一样。
  • with open('ur_file.txt') as f: list_of_ints=[int(e) for e in f]

标签: python python-3.x


【解决方案1】:

欢迎使用 Python。怎么样

dump_here = []
with open('your_file.txt') as my_file:
    for line in my_file:
        dump_here.append(line)

【讨论】:

  • 啊,是的,已经完成了,谢谢
  • 你不需要my_file.close() 这就是上下文管理器的重点。另外,你可以做dump_here = list(my_file)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多