简体   繁体   English

Python的csv阅读器继续读取同一文件

[英]Python's csv reader keep reading the same file

i'm having a problem with the python's csv reader. 我在使用python的csv阅读器时遇到问题。 The problem is that i want to open and read different csv files, but he keeps on reading always the same one. 问题是我想打开并读取不同的csv文件,但是他一直在读取始终相同的文件。

    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. 我什至试图关闭csv文件,但是没有用。

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" 他打开“ EDILIZIA.csv”并编译“ EDILIZIA.xml”,然后应该打开“ EDILE-ARCHIT.csv”并编译其xml,但是当他阅读时,他继续从“ EDILIZIA.csv”中读取。

Here's the .csv files that you need. 这是您需要的.csv文件。

http://pastebin.com/kJhL8HpK 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. 如果您尝试使其先读取EDILIZIA.csv,然后再读取EDILE-ARCHIT.csv,则他将继续使用EDILIZIA.csv来编译xml,但是他应该先打开EDILIZIA.csv,再编译EDILIZIA.xml,然后阅读EDILE-ARCHIT.csv并编译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 如果看一下最终的xml,您将看到EDILE-ARCHIT.xml仅显示EDILIZIA.csv和EDILE-ARCHIT.csv的共同主题。

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. 无论如何,您的代码每次都会读取相应的csv文件,因此该错误不在您认为的位置。

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. 如果我是对的,则orario.csv包含每个课程的时间表(存储在corsi列表中)为三个学期或三年,而corso.csv文件包含课程举行的房间。 So you want to merge the information into an XML file. 因此,您希望将信息合并到XML文件中。

You only forgot one thing: to proceed in orario.csv . 您只忘记了一件事:在orario.csv继续。 Your code wants to merge the very first three anno with the current corso . 您的代码希望将前三个anno与当前corso合并。 To fix it, you have to make two changes. 要修复它,您必须进行两项更改。

First in this for loop header: 首先是这个for循环头文件:

for corso in corsi:

Modify to: 修改为:

for num, corso in enumerate(corsi):

And when you assign lez : 当您分配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: 这段代码产生的结果完全相同,但是它使用Python的XML模块来构建输出文件:

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. 干杯。

I think I may have spotted the cause of your problem. 我想我可能已经发现了您问题的原因。

The second item in your corsi list ends with a full stop. 您的corsi列表中的第二项以corsi结尾。 This means that you will be looking for the file " EDILE-ARCHIT..csv ", which is almost does not exist. 这意味着您将寻找几乎不存在的文件“ EDILE-ARCHIT..csv ”。 When you try and open the file, the open() call will throw an exception, and your program will terminate. 当您尝试打开文件时, open()调用将引发异常,并且程序将终止。

Try removing the trailing full stop, and running it again. 尝试删除尾随的句号,然后再次运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM