how to open a csv file with python?

0

hello everyone I have a code in python that I used to send text messages, this code was passed by the same manufacturer of the product but I do not understand a lot of python, could you tell me how to open the csv file here please I pass the code to you

from subprocess import *;
import json
import urllib
import csv
import sys


web_user = 'admin'

web_pass = 'admin'

lyric_ip = '192.168.1.202'

api_user = 'lyric_api'

api_pass = 'lyric_api'

api_version = '0.01'

url = "http://" + web_user + ":" + web_pass + "@" + lyric_ip + "/cgi-bin/exec"

class Mensaje:
    estado = -1
    contenido = ""
    destino = ""
    message_id = -1;
    def __init__(self, destino, contenido):
        self.estado = 0
        self.destino = destino
        self.contenido = contenido

def queue(self, url, api_user, api_pass, api_version):
    args = urllib.urlencode({'cmd': 'api_queue_sms', 'username': api_user, 'password': api_pass, 'content': self.contenido, 'destination': self.destino, 'api_version': api_version})
    print args
    res = urllib.urlopen(url + '?' + args).read()
    obj = json.loads(res)

    if obj['success']:
        mensajes[i].estado = 0
        self.message_id = obj['message_id']
        print 'Mensaje insertado exitosamente. Ticket: ' + str(obj['message_id'])
        return 1
    else:
        mensajes[i].estado = -1
        print 'Error al insertar mensaje. Codigo de error: ' + obj['error_code']
        return -1

def get_status(self, url, api_user, api_pass, api_version):
    if self.estado == -1 or self.estado >= 2:
        return self.estado

    args = urllib.urlencode({'cmd': 'api_get_status', 'message_id': self.message_id, 'username': api_user, 'password': api_pass, 'api_version': api_version})
    res = urllib.urlopen(url + '?' + args).read()
    obj = json.loads(res)

    if obj['success']:
        self.estado = obj['message_status']
        print 'Ticket: ' + str(self.message_id) + ' Estado: ' + str(self.estado)
        return obj['message_status']
    else:
        print 'Error al consultar estado. Codigo de error: ' + obj['error_code']
        return -1 

file = sys.argv[1]


csvfile = csv.reader(open(file), delimiter=';')

mensajes = range(0)
i = 0
for row in csvfile:
    mensajes.append(Mensaje(row[0], row[1]))
    mensajes[i].queue(url, api_user, api_pass, api_version)
    i = i + 1

all_done = False
while not all_done:
    all_done = True
    for i in range(len(mensajes)):
        estado = mensajes[i].get_status(url, api_user, api_pass, api_version)
        if estado == 0 or estado == 1:
            all_done = False
            break

according to the documentation to execute the escript with the csv tells me this

* To run this Web API test script:
     sms_test file.csv

Where archivo.csv is a file that contains messages to send with the following format:
         num_destination1; content1
      num_destination2; content2

but I do not understand how I have to execute it so that I open the file I hope help thanks

    
asked by ingswsm 02.04.2017 в 16:52
source

0 answers