在Python中迭代CSV文件

时间:2013-11-12 17:12:27

标签: python csv

我正在尝试循环CSV并运行Web请求。我无法通过CSV中的第一行。

CSV正在从Mac Excel导出为10行/ 1列中的10个项目的列表。

def AddTokens(request):
    import csv

    tokenList = []
    output = 0

    apikey = "12345"
    restkey = "12345"
    URL = "https://api.web.com/1/install/"
    headers = {'content-type': 'application/json', 'X-web-Application-Id': apikey, 'X-web-REST-API-Key': restkey}

    with open('/Users/name/Desktop/test.csv', 'rU') as csvfile:
        deviceTokens = csv.reader(csvfile, delimiter=',')

        for token in deviceTokens:
            deviceToken = token[0].replace("/", "")
            deviceType = "ios"
            pushToken = "pushtoken_" + deviceToken
            payload = {"deviceType": deviceType, "deviceToken": deviceToken, "channels": ["", pushToken]}

            r = requests.post(URL, data=json.dumps(payload), headers=headers)

    t = get_template('addpush.html')

    html = t.render(Context({'output': output, 'tokenList': tokenList, 'deviceTokens': deviceTokens, 'token': token}))

    return HttpResponse(html)

0 个答案:

没有答案