0

i'm having a problem with the python's csv reader. The problem is that i want to open and read different csv files, but he keeps on reading always the same one.

    from csv import reader

    alphabet = ["a", "b", "c"]
    for letter in alphabet:
       csv_file = open('/home/desktop/csv/' + letter + '.csv', 'r')
       csv_data = reader(csv_file)

The problem is that he seems to open the other files, but he keep on reading always the first file.

Is there a way to "clean" the reader or make him read another file? I even tried to close the csv file, but it didn't work.

The full code is this

from csv import reader
#Orario
orario_csv_file = '/home/andrea/Scrivania/orario.csv'
orario_csv = open(orario_csv_file)
orario_data = reader(orario_csv)
orario = []
#Corsi
corsi = ["EDILIZIA", "EDILE-ARCHIT", "ELETTRONICA", "TECNOLOGIE_DI_INTERNET", "INFORMATICA", "GESTIONALE", "ENERGETICA", "MECCANICA", "CIVILE_ED_AMBIENTALE", "MEDICA", "ENGINEERING_SCIENCES"]
giorni = ["Lun", "Mar", "Mer", "Gio", "Ven"]



for row in orario_data:
                orario.append(row)
for corso in corsi:
    nome_corso_file = '/home/andrea/Scrivania/xml/' + corso + '.xml'
    nome_corso_xml = open(nome_corso_file, 'wt')
    nome_corso_xml.write('<?xml version="1.0"?>' + "\n")
    nome_corso_xml.write('<orario>' + "\n")
    nome_csv = corso + '_csv'
    nome_csv = '/home/andrea/Scrivania/csv/' + corso + '.csv'
    nome_corso_csv = open(nome_csv, 'rt')
    corso_data = reader(nome_corso_csv)
    nome_corso_xml.write('  <corso name="' + corso + '">' + "\n")
    for a in range(0, 3):
        nome_corso_xml.write('     <anno num="' + str(a+1) + '">' + "\n")
        for j in range(1, 6):
            nome_corso_xml.write('      <giorno name="' + orario[2][j] + '">' + "\n")
            for i in range(3, 12):
                lez = orario[i + a*12][j]
                if lez == "":
                    nome_corso_xml.write('         <lezione>' + "-" + '</lezione>' + "\n")
                else:
                    for riga in corso_data:
                        if riga[0] == lez:
                            if riga[2] == "":
                                nome_corso_xml.write('         <lezione name="' + lez + '">' + riga[1] + '</lezione>' + "\n")
                            else:
                                for g in range(0, len(riga)):
                                    if riga[g].lower() == orario[2][j].lower():
                                        nome_corso_xml.write('         <lezione name="' + lez + '">' + riga[g+1] + '</lezione>' + "\n")
                    nome_corso_csv.seek(0)
            nome_corso_xml.write('      </giorno>' + "\n")
        nome_corso_xml.write('     </anno>' + "\n")
    nome_corso_xml.write('  </corso>' + "\n")
    nome_corso_xml.write('</orario>' + "\n")
    nome_corso_xml.close()

He open the "EDILIZIA.csv" and compile the "EDILIZIA.xml", then he should open the "EDILE-ARCHIT.csv" and compile its xml, but when he read, he keeps on reading from "EDILIZIA.csv"

Here's the .csv files that you need.

http://pastebin.com/kJhL8HpK

If you try to make it read first EDILIZIA.csv and then EDILE-ARCHIT.csv he'll keep on using always the EDILIZIA.csv to compile the xml, but he should firt open EDILIZIA.csv, compile the EDILIZIA.xml, then read the EDILE-ARCHIT.csv and compile the EDILE-ARCHIT.xml.

If you take a look at the final xmls, you'll see that the EDILE-ARCHIT.xml will only display the common subjects of EDILIZIA.csv and EDILE-ARCHIT.csv

wan
  • 1
  • 1
  • 1
    We need to see the code that actually reads from the CSV file, not just the code for opening the file. However, at a guess, I would suspect that you have your indentation wrong, and the code isn't doing what you think it is. – Simon Callan Oct 04 '13 at 08:28
  • The outer `for` line should not be indented. – Milo Oct 04 '13 at 08:48
  • The second `nome_corso_file` line overrides the previously assigned value in the previous line. Makes no sense. – Milo Oct 04 '13 at 08:51
  • 1
    Anyway, I'd recommend using XML etree for XML document creation. Here is a simple example: http://stackoverflow.com/questions/3605680/creating-a-simple-xml-file-using-python – Milo Oct 04 '13 at 08:57
  • Can you provide a few lines of your input csv? – Milo Oct 04 '13 at 10:40
  • When you say that it keeps reading the same file, do you mean that it generates multiple output files, one for each input file, but that they all have the same contents, or that only one output file is generated? – Simon Callan Oct 04 '13 at 11:42
  • I doubt that your code runs at all. I'm trying it and it's full of errors. E.g. `orario` is defined as an empty list, but later you want to get its 2nd index while it is still empty. – Milo Oct 04 '13 at 12:09

2 Answers2

1

It took a long time to figure out what you are doing here. To tell the truth your code is a mess - there are many unused variables and lines that make no sense at all. Anyway, your code reads the appropriate csv file each time, thus the error is not where you thought it was.

If I am right, orario.csv contains the timetable of each course (stored in corsi list) for three semesters or years, and the corso.csv files contain the room where subjects are held. So you want to merge the information into an XML file.

You only forgot one thing: to proceed in orario.csv. Your code wants to merge the very first three anno with the current corso. To fix it, you have to make two changes.

First in this for loop header:

for corso in corsi:

Modify to:

for num, corso in enumerate(corsi):

And when you assign lez:

lez = orario[i + a*12][j]

Modify to:

lez = orario[i + a*12*(num+1)][j]

Now it should work.

This code produces exactly the same result, but it uses Python's XML module to build the output file:

from csv import reader
import xml.etree.cElementTree as ET
import xml.dom.minidom as DOM

corsi = ["EDILIZIA", "EDILE-ARCHIT", "ELETTRONICA", "TECNOLOGIE_DI_INTERNET", "INFORMATICA", "GESTIONALE", "ENERGETICA", "MECCANICA", "CIVILE_ED_AMBIENTALE", "MEDICA", "ENGINEERING_SCIENCES"]

with open('orario.csv', 'r') as orario_csv:
    orario = reader(orario_csv)
    orario_data = [ row for row in orario ]

for num, corso in enumerate(corsi):
    with open(corso + '.csv', 'r') as corso_csv:
        corso_raw = reader(corso_csv)
        corso_data = [ row for row in corso_raw ]
    root_elem = ET.Element('orario')
    corso_elem = ET.SubElement(root_elem, 'corso')
    corso_elem.set('name', corso)
    for anno in range(0, 3):
        anno_elem = ET.SubElement(corso_elem, 'anno')
        anno_elem.set('num', str(anno + 1))
        for giorno in range(1, 6):
            giorno_elem = ET.SubElement(anno_elem, 'giorno')
            giorno_elem.set('name', orario_data[2][giorno])
            for lezione in range(3, 12):
                lez = orario_data[lezione + anno * 12 * (num + 1)][giorno]
                if lez == '':
                    lezione_elem = ET.SubElement(giorno_elem, 'lezione')
                    lezione_elem.text = '-'
                else:
                    for riga in corso_data:
                        if riga[0] == lez:
                            if riga[2] == '':
                                lezione_elem = ET.SubElement(giorno_elem, 'lezione')
                                lezione_elem.set('name', lez)
                                lezione_elem.text = riga[1]
                            else:
                                for g in range(0, len(riga)):
                                    if riga[g].lower() == orario_data[2][giorno].lower():
                                        lezione_elem = ET.SubElement(giorno_elem, 'lezione')
                                        lezione_elem.set('name', lez)
                                        lezione_elem.text = riga[g + 1]
    with open(corso + '_new.xml', 'w') as corso_xml:
        xml_data = DOM.parseString(ET.tostring(root_elem, method = 'xml')).toprettyxml(indent = '    ')
        corso_xml.write(xml_data)

Cheers.

Milo
  • 655
  • 1
  • 12
  • 25
  • Thank you for the advices and for the time, but it still doesn't work. Seems that there are some problems, because something changed but not in the right way. Thank you anyway. I'll try to use the Python's XML module. – wan Oct 06 '13 at 15:42
0

I think I may have spotted the cause of your problem.

The second item in your corsi list ends with a full stop. This means that you will be looking for the file "EDILE-ARCHIT..csv", which is almost does not exist. When you try and open the file, the open() call will throw an exception, and your program will terminate.

Try removing the trailing full stop, and running it again.

Simon Callan
  • 3,020
  • 1
  • 23
  • 34
  • Tried to remove the full stop both from the .csv file and from the item in the list, but nothing changed. – wan Oct 04 '13 at 11:23