Search code examples
csvrevit-apirevitrevitpythonshell

How to get CSV to work in in RevitPythonShell?


Has anyone figured out how to get CSV (or any other package) to work in RevitPythonShell? I've only been able to get Excel from Interop to work.

When I try running csv in RPS, the terminal executes and shows no error or any kind of feed back, and the file is not created either.

This is the basic code I'm trying to run which comes from a tutorial on CSV I believe.

with open('mycsv2.csv', 'w') as f:
     fieldnames = ['column1', 'column2', 'column3']
     thewriter = csv.DictWriter(f, fieldnames=fieldnames)
     thewriter.writeheader()
     for i in range(1, 10):
         thewriter.writerow({'column1':'one', 'column2':'two', 'column3':'three'})

I find CSV much more user friendly and easier to understand than Interop Excel. I believe I've read its doable somewhere but of course I cant find the source now.

All help, tips, or tricks are appreciated.


Solution

  • I can get it to work by supplying the full path name to the open function, so it looks like (showing full path to my Documents Folder):

    import csv
    with open(r'C:\Users\callum\Documents\mycsv2.csv', 'w') as f:
         fieldnames = ['column1', 'column2', 'column3']
         thewriter = csv.DictWriter(f, fieldnames=fieldnames)
         thewriter.writeheader()
         for i in range(1, 10):
             thewriter.writerow({'column1':'one', 'column2':'two', 'column3':'three'})
    

    Let me know if that does the trick!