Pregunta

I am doing the Kaggle Titanic beginner contest. I generally work in Spyder IDE, but I came across a weird issue. The expected output is supposed to be 418 rows. When I run the script from terminal the output I get is 418 rows (as expected). When I run it in Spyder IDE the output is 408 rows not 418. When I re-run it in the current python process, it outputs the expected 418 rows. I posted a redacted portion of the code that has all of the relevant bits. Any ideas?

import csv
import numpy as np

csvFile = open("/train.csv","ra")
csvFile = csv.reader(csvFile)

header = csvFile.next()

testFile = open("/test.csv","ra")
testFile = csv.reader(testFile)
testHeader = testFile.next()

writeFile = open("/gendermodelDebug.csv", "wb")
writeFile = csv.writer(writeFile)


count = 0
for row in testFile:

if row[3] == 'male':
    do something to row
    writeFile.writerow(row)
    count += 1
elif row[3] == 'female':
    do something to row
    writeFile.writerow(row)
    count += 1
else:
    raise ValueError("Did not find a male or female in %s" % row)
¿Fue útil?

Solución

You may try:

writeFile = open("/gendermodelDebug.csv", "w")          #'w' instead of 'wb'

if you use 'wb' binary output then row has no meaning.

.... Remember that:
after you finished your work, you need to close the opened file using:

writeFile.close()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top