1

I am using GAE to host a website which needs inputs from a CSV file. After this csv file has been uploaded, I will convert it to a table. However, I met the problem about Mac and Windows compatibility issue. The CSV file generated in Mac will not be recognized, and I got the error:

new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

Here is my Python CODE

def loop_html(thefile):
    reader = csv.reader(thefile.file)   
    header = reader.next()
    i=1
    iter_html=""
    for row in reader:
        iter_html = iter_html +html_table(row,i)  #generate inputs table
        i=i+1

def html_table(row_inp,iter):
    mai_temp=float(row_inp[0])

    Input_header="""<table border="1">
                        <tr><H3>Batch Calculation of Iteration %s</H3></tr><br>
                        <tr>
                            <td><b>Input Name</b></td>
                            <td><b>Input value</b></td>
                            <td><b>Unit</b></td>
                        </tr>"""%(iter)
    Input_mai="""<tr>
                    <td>Mass of Applied Ingredient Applied to Paddy</td>
                    <td>%s</td>
                    <td>kg</td>
                </tr>""" %(mai_temp) 
    Inout_table = Input_header+Input_mai
    return Inout_table  

Later I changed the code 'reader = csv.reader(thefile.file) ' to 'reader = csv.reader(open(thefile.file,'U')) ' which gave me different types of error:

TypeError: coercing to Unicode: need string or buffer, cStringIO.StringO found

Can anyone take a look at my code and give me some suggestions? Thanks!

1 Answer 1

1

I just found a solution. 'splitlines()' will handle the new line issue. Here is the source.

reader = csv.reader(thefile.file.read().splitlines())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.