Skip to main content
deleted 20 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Mapping a CSV with UTF-8 encoding to JsonJSON in Python

I needed to create a jsonJSON from a CSV with predefined headers. I'm downloading an attachment from JIRA (REST API), parsing the file and then mapping each value to a dictionary. The CSV has UTF-8 values.

I solved what I needed, but I would like to learn how to do this properly, and maybe someday become a real programmer.

:)

EDIT: The CSV has 7 columns, in. In order to identify which one I'm parsing, I've added the variable "a"a which increments each time, when. When I reach the 7th column I reset the value to 0, until the end of the file.

EDIT: The CSV file doesn't contain headers for each column.

Mapping a CSV with UTF-8 encoding to Json in Python

I needed to create a json from a CSV with predefined headers. I'm downloading an attachment from JIRA (REST API), parsing the file and then mapping each value to a dictionary. The CSV has UTF-8 values.

I solved what I needed, but I would like to learn how to do this properly, and maybe someday become a real programmer.

:)

EDIT: The CSV has 7 columns, in order to identify which one I'm parsing I've added the variable "a" which increments each time, when I reach the 7th column I reset the value to 0, until the end of the file.

EDIT: The CSV file doesn't contain headers for each column.

Mapping a CSV with UTF-8 encoding to JSON in Python

I needed to create a JSON from a CSV with predefined headers. I'm downloading an attachment from JIRA (REST API), parsing the file and then mapping each value to a dictionary. The CSV has UTF-8 values.

I solved what I needed, but I would like to learn how to do this properly, and maybe someday become a real programmer.

The CSV has 7 columns. In order to identify which one I'm parsing, I've added the variable a which increments each time. When I reach the 7th column I reset the value to 0, until the end of the file.

The CSV file doesn't contain headers for each column.

added 63 characters in body
Source Link

EDIT: The CSV file doesn't contain headers for each column.

EDIT: The CSV file doesn't contain headers for each column.

Post Migrated Here from stackoverflow.com (revisions)
Source Link

Mapping a CSV with UTF-8 encoding to Json in Python

I'm not a programmer, just a guy who needs to solve particular things using code.

I needed to create a json from a CSV with predefined headers. I'm downloading an attachment from JIRA (REST API), parsing the file and then mapping each value to a dictionary. The CSV has UTF-8 values.

# First I prepare my dic
    extracted_info = []
    mydic = {   'Ticket': None,
                'Dominio': None,
                'Tipo': None,
                'Ruta': None,
                'Version': None,
                'Componente': None,
                'Comentario': None
                }

    # Mapping values from CSV to Dictionary
    #
    def mappingThoseValues(d, a):
       # print i
        if d != "" :
            if a == 1 :
                mydic['Ticket'] = d
            elif a == 2:
                mydic['Dominio'] = d
            elif a == 3:
                mydic['Tipo'] = d
            elif a == 4:
                mydic['Ruta'] = d
            elif a == 5:
                mydic['Version'] = d
            elif a == 6:
                mydic['Componente'] = d
            elif a == 7:
                mydic['Comentario'] = d
                extracted_info.append(mydic.copy())    
    
    # Parse CSV
    #
    # If URL isn't empty
    if attachment_final_url != "" :
        with open(runbookname) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t')
            a= 0
            for row in spamreader:
                for i in row :
                    d = i.decode('iso-8859-1').encode('utf8')
                    a = a + 1
                    mappingThoseValues(d,a)
                    if a == 7 :
                        a = 0
        csvfile.close()
    else :
        status_compara_rn_jira = error_format_tab + 'ERROR: No attachments in : ' + myTicket

I solved what I needed, but I would like to learn how to do this properly, and maybe someday become a real programmer.

:)

EDIT: The CSV has 7 columns, in order to identify which one I'm parsing I've added the variable "a" which increments each time, when I reach the 7th column I reset the value to 0, until the end of the file.