Frage

I have a TextCtrl parsing the familuar csv format like:

"ID","X","Y","Z"
1,199,486.5,13
2,200,485.3,12

I do some regex and afterwards, reload my CtrlList to see the changes. You convert csv format to list set like:

[['ID', 'X', 'Y', 'Z'], ['2', '200', '485.3', '12'], ['1', '199', '486.5', '13']]

For now I'm implimenting:

datafile = open(message, 'r')  # message receiever from dispatcher 
datareader = csv.reader(datafile) 
data = [] 
for row in datareader: 
    data.append(row)   # Traceback - IOError: [Errno 22] invalid mode ('r') or filename

No matter what mode I use I still get this error. I need to understand why.

War es hilfreich?

Lösung

If you are passing an object or a string or a list or whatever, then you can NOT open it. Python's open() is for opening files or file-like objects. You would be better off wrapping the message object inside of StringIO to convert it into an file-like object. Then you might be able to use the csv module to read it. Or you could just pass the file object to the csv module itself via the dispatcher rather than whatever it is that you're currently passing.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top