【问题标题】:Python increment pulling from an array in a loopPython 在循环中递增从数组中拉取
【发布时间】:2013-05-09 14:21:00
【问题描述】:

我正在尝试创造一些东西

  • 读取文件
  • 将任何东西分成一个数组
  • 使用数组中的第一个字符串做一些事情
  • 完成后返回第二个数组做点什么
  • 不断重复此过程,直到阵列完成。

目前为止

users = open('users.txt', "r")
userL = users.read().splitlines()

我想要它做的是打开文本文件,每个字符串已经分隔了 1 行,然后让 Python 部分将其放入数组中,获取第一个字符串并将其设置为变量。从那里,该变量将用于 xbox.com 的 URL。

在它检查之后,我会让一些 JSON 读取页面并查看我拥有的玩家标签列表是否正在使用,如果正在使用,它将返回数组并转到第二个字符串并检查。这需要一个不断检查玩家代号的循环。如果它确实在数组(来自文本文件)中找到了一个未使用的玩家标签,它会将其保存到另一个名为“可用游戏标签”的文本文件中并继续前进。

我想要它做什么(在 cmets 中请求)

  • 打开程序
  • 让它读取我创建的用户名文本文件
  • 让程序测试 Xbox 的玩家代号查看器链接末尾的每个程序
  • JSON 读取页面,如果它包含名称被使用的信息,它会返回列表并使用页面上的下一个玩家代号。
  • 继续这样做
  • 记录所有有效的玩家代号并保存到一个文本文件中。
  • 退出

这样做的问题是我不知道如何返回文件并访问刚刚测试的行之后的行并继续此模式直到文件被完全读取。

【问题讨论】:

  • 欢迎来到 Stack Overflow!看起来您希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只会在发布者已经尝试自己解决问题时提供帮助。展示这项工作的一个好方法是包含您迄今为止编写的代码、示例输入(如果有的话)、预期输出和您实际获得的输出(控制台输出、堆栈跟踪、编译器错误 - 不管是什么适用的)。您提供的详细信息越多,您可能收到的答案就越多。
  • 所以基本上你想要一个像样的玩家代号?

标签: python increment


【解决方案1】:

使用 for 循环:

with open("users.txt") as f:
    for line in f:
        # do whatever with the line

例如,为了实现您的目标,您可能会这样做:

# import our required modules
import json
import urllib2

# declare some initial variables
input_file = "users.txt"
output_file = "available_tags.txt"
available_tags = [] # an empty list to hold our available gamertags

# open the file
with open(input_file) as input_f:
    # loop through each line
    for line in input_f:
        # strip any new line character from the line
        tag = line.strip()
        # set up our URL and open a connection to the API
        url = "http://360api.chary.us/?gamertag=%s" % tag
        u = urllib2.urlopen(url)
        # load the returned data as a JSON object
        data = json.loads(u.read())
        # check if the gamertag is available
        if not data['GamertagExists']:
            # print it and add it to our list of available tags if so
            print "Tag %s is available." % tag
            available_tags.append(tag)
        else:
            print "Tag %s is not available." % tag #otherwise

# check that we have at least one valid tag to store
if len(available_tags) > 0:
    # open our output file
    with open(output_file, "w") as output_f:
            # loop through our available tags
            for tag in available_tags:
                # write each one to the file
                output_f.write("%s\n" % tag)
else:
    print "No valid tags to be written to output file."

【讨论】:

  • 非常感谢。这正是我想要的。感谢您的注释,我是 Python 新手,所以这对我有很大帮助。
  • 我将如何实现另一个文件以读入此语句?
  • 要读取第二个或无限数量的输入文件,创建一个文件名列表并循环遍历它,执行我已经为每个文件编写的代码块。请务必重新缩进当前代码块。
【解决方案2】:

为了帮助您开始,以下代码将按顺序从头到尾读取整个文件,并单独打印每一行:

with open(r"path/to.file.txt") as fin:
    for line in fin.readlines():
        print(line) # Python 2.7: Use 'print line' instead

如果您需要从每个字符串中删除尾随的新行,请使用.strip()。

要将数据写入文件,请使用以下内容:

with open(r"path/to/out/file.txt", "w") as fout:
    fout.writelines(data_to_write)

【讨论】:

  • 我知道如何做那部分,除了现在我希望它一次读取输出的每一行,并且每当读取一行时,将其设置为变量并不断切换变量
  • 这就是 for 循环的作用,我建议您阅读它们。我整理了一个程序,它可以满足您的需求,并将其添加到我自己的答案中。我不知道您是否想使用与我相同的 API,我仍然认为您应该阅读诸如循环之类的控制流语句,因为它们对于任何语言的编程都是非常基础的。
猜你喜欢
  • 1970-01-01
  • 2018-05-09
  • 2015-01-23
  • 2019-03-09
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 2013-12-26
相关资源
最近更新 更多