#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    For loop don't work


    hi i am writing to CSV file succesfully. i am doint it like that but when i try to read, the for loop doen't itterate. here is the code (first the code and the module i wrote just after):

    import os
    import csv
    import HTMLcreate

    path='c:/temp'


    FailedCSV = csv.writer(open('FailedTests.csv', 'wb'), delimiter=',') #might consider adding dialect=execfile

    listing = os.listdir(path)
    TotalFailed=0
    for infile in listing: #loop on all the files in the directory
    str_in = path + '/' + infile #create full path file name
    buffer=open(str_in).read() #puts all text in buffer
    i=1
    CaseIndex = 'Case ' + repr(i) #case + number first time case 1. repr() converts to string
    CaseNextIndex = 'Case ' + repr(i+1)
    while CaseIndex in buffer:
    CI=buffer.find(CaseIndex) #finding the current case place in buffer
    if CaseNextIndex in buffer: #finding next case place of EOF
    CNI=buffer.find(CaseNextIndex)
    else:
    CNI=buffer.find('')
    CurrentCaseBuffer=buffer[CI:CNI]
    #print CurrentCaseBuffer
    if '+++++ FAIL' in CurrentCaseBuffer: #if the current case is a failed case
    TotalFailed+=1
    string=CurrentCaseBuffer[:CurrentCaseBuffer.find(')')]
    parentheses=string.count('(')
    TestNumber=CurrentCaseBuffer[len(CaseIndex)+1: CurrentCaseBuffer.find(')')]
    if parentheses==2:
    TestNumber+=')'
    description=CurrentCaseBuffer[CurrentCaseBuffer.find(':')+2:CurrentCaseBuffer.find('\n')]
    list1= [TotalFailed, TestNumber,description,infile[:len(infile)-4]]
    FailedCSV.writerow(list1)
    i+=1
    CaseIndex = 'Case ' + repr(i) + ' '
    CaseNextIndex = 'Case ' + repr(i+1)

    HTMLcreate.csv2html('FailedTests.csv','index.html')


    import csv

    def csv2html(csvfile, htmlfile):

    my_csv_file= open(csvfile,'rb')
    my_csv_file.seek(0,0)
    my_csv = csv.reader(my_csv_file)



    my_html = open(htmlfile,'w')

    #Write the opening table tag to the html file:

    my_html.write('<table>\n')

    #Write a loop to loop through every line in the CSV file:
    my_csv.readrow
    for line in my_csv:
    my_html.write('<tr>\n')
    my_html.write('</tr>\n')
    for entry in line:#Write an array-traversing loop between the two table row tags. Inside the loop, write each entry to the html file, enclosing it in table data tags:
    my_html.write('<td>' + entry + '</td>\n')

    my_html.write("</table>\n")
    my_html.close()
    my_csv_file.close()
    return
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Please embed your program in [ code] program [ /code] tags (without the spaces). Editing your first message might work.

    Where you have '/' in your program, if it is a path separation character,
    os.sep
    is portable and you should use it instead.

    You have a program statement
    my_csv.readrow
    that doesn't do anything. Did you have an unfinished thought?

    Finally, your complaint seems to be that
    Code:
        for entry in line:#Write an array-traversing loop ...
            my_html.write('<td>' + entry + '</td>\n')
    doesn't do anything. I suggest that just before this loop you insert a print statement such as
    print('line is <'+str(line)+'>')
    to help yourself debug the code.

    Otherwise, we can only guess your intended program indentation, and we haven't got your input files to work with. So, good luck.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    39
    Rep Power
    3
    With the way you have the opened the CSV file for writing there is no easy way of closing or flushing the file; instead you are relying on the file being closed automatically when FailedCSV goes out of scope. The rows written to the CSV may well still be buffered when you try reading it in.

    Instead try changing the opening of the file to
    Code:
    FailedCSV_file = open('FailedTests.csv', 'wb', newline='')
    FailedCSV = csv.writer(FailedCSV_file, delimiter=',')
    You can then add FailedCSV_file.close() before you call the csv2html function.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Even when out of scope variables still have to wait for garbage collection before they're released.

    The preferred method for opening files in modern pythons is
    Code:
    with open(filename,'mode') as my_stream:
        # use the file object my_stream
    # done with my_stream
    Closes the file for you automatically.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo