2013-05-09 22 views
1

我想創造的東西,會Python的增量在一個循環從陣列中拔出

  • 閱讀文件
  • 獨立以往的東西放在一個陣列
  • 使用數組中的第一個字符串,做東西
  • 完成後回到第二個陣列並做點什麼
  • 繼續重複這個過程直到數組完成。

到目前爲止,我有

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

我希望它做的恰恰是打開文本文件,該文件已經分離每串1號線,然後讓Python的部分它放入一個數組,獲取第一個字符串並將其設置爲變量。從那裏變量將被用於xbox.com的URL。

它檢查後,我會有一些JSON讀取頁面,看看我有的gamertag列表是否正在使用,如果正在使用它,它將返回到數組,並轉到第二個字符串並檢查。這需要成爲一個不斷檢查玩家標籤的循環。如果它在數組中找到了一個未使用的數組(來自文本文件),它會將其保存到另一個標題爲「可用的遊戲標籤」的文本文件中並繼續前進。

我想要它做的(在意見中的要求)

  • 開放計劃
  • 有它讀我創建的用戶名的文本文件
  • 有程序測試每個程序在到底是什麼xbox的gamertag查看器鏈接
  • JSON讀取該頁面,如果它包含該名稱的信息,則返回列表並使用頁面上的下一個gamertag。
  • 保持這樣做
  • 記錄工作並保存到文本文件的所有gamert標記。
  • 退出

在這樣做,是我不知道怎麼回去的文件,它只是測試的一前一後訪問該線,並繼續這種模式,直到該文件是完全讀出的問題。

+5

歡迎堆棧溢出!看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的細節越多,您可能會收到的答案就越多。 – 2013-05-09 14:29:46

+0

所以基本上你想要一個體面的gamertag? – dwerner 2013-05-09 14:55:58

回答

0

使用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." 
+0

非常感謝。這正是我正在尋找的。感謝您的注意,我是Python的新手,所以這對我有很大的幫助。 – user2364549 2013-05-10 00:40:19

+0

我將如何實現另一個文件來讀入此聲明? – user2364549 2013-05-10 01:58:34

+0

讀取輸入文件的第二或無窮多個,創建文件名和循環通過它的列表,執行我已經爲每個文件編寫的代碼塊。確保重新加載當前的代碼塊。 – 2013-05-10 15:38:36

0

爲了讓您一開始,下面的代碼將從頭到尾閱讀整個文件的順序來完成,並分別打印每行:

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

如果你需要從每個字符串中刪除後新生產線,使用.strip()

要將數據寫入一個文件,使用類似以下內容:

​​
+0

我明白如何做那部分,除了現在我希望它讀取每次輸出一行並且每次讀取都將其設置爲變量並保持切換變量外的每行。 – user2364549 2013-05-09 22:30:39

+0

這就是for循環的作用,我建議你閱讀它們。我編寫了一個你想要的程序,並將其添加到我自己的答案中。我不知道你是否想要使用和我一樣的API,我仍然認爲你應該閱讀諸如循環這樣的控制流語句,因爲它們對於用任何語言進行編程都非常重要。 – 2013-05-09 23:24:39