Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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_List - Fatal编程技术网

Python 试图从文件中读取数据并将每行转换为列表,但它跳过了一些值

Python 试图从文件中读取数据并将每行转换为列表,但它跳过了一些值,python,list,Python,List,我正在尝试制作一个简单的基于文本的游戏,我需要存储玩家的分数。因此,我制作了一个小脚本,可以从文件中获取数据,并且(假设)可以这样做: 循环遍历每个单词,并将该单词添加到其行列表中 当程序点击新行时,将行列表添加到包含所有内容的常规列表中 这是我的代码: def readData(path): file = open(path, 'r') data = [] curWord = '' curLineData = [] while True:

我正在尝试制作一个简单的基于文本的游戏,我需要存储玩家的分数。因此,我制作了一个小脚本,可以从文件中获取数据,并且(假设)可以这样做:

  • 循环遍历每个单词,并将该单词添加到其行列表中
  • 当程序点击新行时,将行列表添加到包含所有内容的常规列表中
这是我的代码:

def readData(path):
    file = open(path, 'r')
    data = []
    curWord = ''
    curLineData = []

    while True:
        char = file.read(1)
        if not char == '\n':
            if not char == ' ':
                curWord = curWord.join(char)
            else:
                curLineData.append(curWord)
                curWord = ''
        else:
            data.append(curLineData)
            curLineData = []
            curWord = ''

        if not char:
            break

    file.close()
    return data
以下是file.txt的内容:

p1 1 4
p2 2 18
p3 3 3
我的问题是:无论何时运行代码,都会得到以下结果:

[['1', '1'], ['2', '2'], ['3', '3']]
当我期望得到:

[['p1', '1', '4'], ['p2', '2', '18'], ['p3', '3', '3']]
问题是,它会跳过第一个单词,添加第二个单词,然后重复第一个单词,重复找到的单词数量。经过一些测试,我还发现,如果最后有一个空行,它只会添加最后一行


我很想知道我做错了什么(除了愚蠢之外),如果你能告诉我如何解决它,那就太好了:)

你可以做如下事情:

def readData(path):
    with open(path, 'r') as myfile:
        lines = myfile.readlines()
    return [line.strip().split(' ') for line in lines]
您的代码与此代码之间存在一些差异:

  • 处理文件时,最好使用
    with
    语句()
  • 最好不要对变量使用名称
    file
    ,因为它是内置类型
  • 您可以使用
    readlines
    方法返回行列表
  • 您可以使用
    split
    方法将每一行拆分为一个列表
  • 您可以使用列表理解来构建最终列表

您可以执行以下操作:

def readData(path):
    with open(path, 'r') as myfile:
        lines = myfile.readlines()
    return [line.strip().split(' ') for line in lines]
您的代码与此代码之间存在一些差异:

  • 处理文件时,最好使用
    with
    语句()
  • 最好不要对变量使用名称
    file
    ,因为它是内置类型
  • 您可以使用
    readlines
    方法返回行列表
  • 您可以使用
    split
    方法将每一行拆分为一个列表
  • 您可以使用列表理解来构建最终列表

您可以执行以下操作:

def readData(path):
    with open(path, 'r') as myfile:
        lines = myfile.readlines()
    return [line.strip().split(' ') for line in lines]
您的代码与此代码之间存在一些差异:

  • 处理文件时,最好使用
    with
    语句()
  • 最好不要对变量使用名称
    file
    ,因为它是内置类型
  • 您可以使用
    readlines
    方法返回行列表
  • 您可以使用
    split
    方法将每一行拆分为一个列表
  • 您可以使用列表理解来构建最终列表

您可以执行以下操作:

def readData(path):
    with open(path, 'r') as myfile:
        lines = myfile.readlines()
    return [line.strip().split(' ') for line in lines]
您的代码与此代码之间存在一些差异:

  • 处理文件时,最好使用
    with
    语句()
  • 最好不要对变量使用名称
    file
    ,因为它是内置类型
  • 您可以使用
    readlines
    方法返回行列表
  • 您可以使用
    split
    方法将每一行拆分为一个列表
  • 您可以使用列表理解来构建最终列表

str.join
不会在调用该方法的字符串末尾追加字符串;它使用调用程序作为“分隔符”,将参数的每个元素连接在一起,以
连接
。也就是说,您似乎认为,
“foo.join”(“bar”)
会产生
“foobar”
,而实际上它会产生
“fooafoor”

更好的方法是逐行读取文件,并使用空格作为分隔符将每行拆分为单词:

def readData(path):
    rv = []
    with open(path, 'r') as file:
        for line in file:
            words = line.split()
            rv.append(words)
    return rv

str.join
不会在调用该方法的字符串末尾追加字符串;它使用调用程序作为“分隔符”,将参数的每个元素连接在一起,以
连接
。也就是说,您似乎认为,
“foo.join”(“bar”)
会产生
“foobar”
,而实际上它会产生
“fooafoor”

更好的方法是逐行读取文件,并使用空格作为分隔符将每行拆分为单词:

def readData(path):
    rv = []
    with open(path, 'r') as file:
        for line in file:
            words = line.split()
            rv.append(words)
    return rv

str.join
不会在调用该方法的字符串末尾追加字符串;它使用调用程序作为“分隔符”,将参数的每个元素连接在一起,以
连接
。也就是说,您似乎认为,
“foo.join”(“bar”)
会产生
“foobar”
,而实际上它会产生
“fooafoor”

更好的方法是逐行读取文件,并使用空格作为分隔符将每行拆分为单词:

def readData(path):
    rv = []
    with open(path, 'r') as file:
        for line in file:
            words = line.split()
            rv.append(words)
    return rv

str.join
不会在调用该方法的字符串末尾追加字符串;它使用调用程序作为“分隔符”,将参数的每个元素连接在一起,以
连接
。也就是说,您似乎认为,
“foo.join”(“bar”)
会产生
“foobar”
,而实际上它会产生
“fooafoor”

更好的方法是逐行读取文件,并使用空格作为分隔符将每行拆分为单词:

def readData(path):
    rv = []
    with open(path, 'r') as file:
        for line in file:
            words = line.split()
            rv.append(words)
    return rv

通常,如果可以的话,对文件中的行执行
:…
,更符合python。每次迭代将给出文件的下一行。请将所需的输出记录为well@ari好啊我将尝试这样做并共享结果。@user1767754所需的输出是['p1','1','4'],['p2','2','18'],['p3','3','3']。我将更新这个问题,如果可以的话,对文件中的行执行
:…
,通常更像python。每次迭代将给出文件的下一行。请将所需的输出记录为well@ari好啊我将尝试这样做并共享结果。@user1767754所需的输出是['p1','1','4'],['p2','2','18'],['p3','3','3']。我将更新这个问题,如果可以的话,对文件中的行执行
:…
,通常更像python。每次迭代将给出文件的下一行。请将所需的输出记录为well@ari好啊我将尝试这样做,并分享res