Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中使用读取线数据到数组中的问题_Python_Arrays_List - Fatal编程技术网

python中使用读取线数据到数组中的问题

python中使用读取线数据到数组中的问题,python,arrays,list,Python,Arrays,List,这是我的密码 arr = [] inp = open ("test.txt","r") for line in inp.readlines(): for i in line.split(): arr.append(i) print arr arr的输出是[],如果我试图打印arr[0]或任何它说索引超出范围的内容,我会尝试从文本中提取数据,并将其放入数组中供使用,甚至是一个列表中,我可以从中读取数据。我尝试了很多方法,这在我的错误观点中似乎是最容易的,我想lol 既然我

这是我的密码

arr = []
inp = open ("test.txt","r")
for line in inp.readlines():
    for i in line.split():
        arr.append(i)
print arr
arr的输出是[],如果我试图打印arr[0]或任何它说索引超出范围的内容,我会尝试从文本中提取数据,并将其放入数组中供使用,甚至是一个列表中,我可以从中读取数据。我尝试了很多方法,这在我的错误观点中似乎是最容易的,我想lol


既然我在这里询问,我也想在文档中搜索以say“start”开头的一行,但将数据放入数组或列表中可能是最好的方法,谢谢您的帮助,我也是python新手,现在已经很晚了,所以这是我最后的办法,代码没问题。
test.txt
为空,或者这段代码不正确,或者您从中编辑了一些重要内容。

代码正常。
test.txt
为空,或者这段代码不正确,或者您从中编辑了一些重要内容。

我假设您希望列表包含文件的每一行。在这种情况下,循环中不需要额外的“if”,因为readlines方法已经一次读取一行文件了

如果文件为空,则数组将为空

另外,要仅将以“start”开头的行添加到数组中,您可以执行以下操作:

arr = []
inp = open("test.txt","r")
for line in inp.readlines():
    if line.startswith("start"):
        arr.append(line)
print arr
甚至更短的代码:

arr = [x for x in open('text.txt') if x.startswith('start')]

我假设您希望列表包含文件的每一行。在这种情况下,循环中不需要额外的“if”,因为readlines方法已经一次读取一行文件了

如果文件为空,则数组将为空

另外,要仅将以“start”开头的行添加到数组中,您可以执行以下操作:

arr = []
inp = open("test.txt","r")
for line in inp.readlines():
    if line.startswith("start"):
        arr.append(line)
print arr
甚至更短的代码:

arr = [x for x in open('text.txt') if x.startswith('start')]

无需调用
.readlines()
open()
函数返回一个用作迭代器的文件句柄,它将一次返回一行。另外,如果您使用的是现代Python,那么您就有了
,最好将其用于这类事情。因此:

arr = []
with open("test.txt", "r") as inp:
    for line in inp:
        for word in line.split():
            arr.append(word)
print arr
这对我来说非常好,当我测试它时,它对我有效

另外,我忍不住要把它作为一行列表:

arr = [word for line in open("test.txt") for word in line.split()]
print arr
呃,即使是列表理解也应该使用
with()
。因此:

with open("test.txt", "r") as inp:
    arr = [word for line in inp for word in line.split()]
print arr

无需调用
.readlines()
open()
函数返回一个用作迭代器的文件句柄,它将一次返回一行。另外,如果您使用的是现代Python,那么您就有了
,最好将其用于这类事情。因此:

arr = []
with open("test.txt", "r") as inp:
    for line in inp:
        for word in line.split():
            arr.append(word)
print arr
这对我来说非常好,当我测试它时,它对我有效

另外,我忍不住要把它作为一行列表:

arr = [word for line in open("test.txt") for word in line.split()]
print arr
呃,即使是列表理解也应该使用
with()
。因此:

with open("test.txt", "r") as inp:
    arr = [word for line in inp for word in line.split()]
print arr

谢谢你的作品,nicly不认为它比C简单。开始使用它时,我非常喜欢python而不是C。谢谢你的作品,nicly不认为它比C简单。开始使用它时,我非常喜欢python而不是C。我觉得很愚蠢,我有文字在里面,但我猜它不知怎么被删除了,所以我什么都没有得到,我想我是这样的做对了我觉得很愚蠢我有文字在里面但我猜它不知怎么被抹掉了所以我什么都没得到我认为我做对了我按顺序做是因为我很早就被教过了(当我从一本八年级的书上教我的基本功时,所以我一直都是这样做的)还有dosent flow和yeti的其他代码一起做tab顺序,因为我很早就被教过(当我从8年级开始教我的自学时,我一直都是这样做的),还有dosent flow和其他代码一起做