1

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.

Cflux
  • 1,423
  • 3
  • 19
  • 39
  • I can run your code and it works fine - as long as I supply a full path to the `open` function rather than just a filename. Does that help? – Callum Jun 25 '19 at 02:49
  • I never tried opening an existing file. I'll give that a go. Are you able to open a new file and execute the code? when i write in any other IDE or through python IDLE, or even through command line, it works but it wont work through RPS. – Cflux Jun 25 '19 at 02:54
  • When you open a file with the `w` tag, it creates the file if it doent exist - which is what I did. – Callum Jun 25 '19 at 03:10
  • what does your full path to the open function look like? that sounds like it will probably help me if i can take a shot at reverse engineering it – Cflux Jun 25 '19 at 03:14

1 Answers1

2

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!

Callum
  • 578
  • 3
  • 8
  • it works! it creates blank lines inbetween each row but i'll take it. what was the r' in the filepath used for? it wasnt necessary for me – Cflux Jun 25 '19 at 03:30
  • 1
    I learnt that from the legendary Daren Thomas - it means 'Raw String', so you can enter file paths without having to pair the backslashes. The alternative is to write `'C:\\Users\\callum\\Documents\\mycsv2.csv'` – Callum Jun 25 '19 at 03:44