d12n
d12n

Reputation: 861

Python not creating and writing to csv

I am using Spyder and accessing the reddit API to get some data and write it to csv, when I print out the lines, everything works fine, but then the csv file just doesn't get created, I tried many things but nothing seems to work, and a very similar piece of code worked out fine and I got the csv file, so I have no idea what the problem is.

with open('finalplswork.csv', 'wb') as fl:
    writr = csv.writer(fl)   
    for subid in idsss:
        submission = r.get_submission(submission_id=subid)
        created_utc=submission.created_utc
        created_date_utc = datetime.fromtimestamp(created_utc)
        data=(subid, created_utc, created_date_utc)
        writr.writerow(data)

Upvotes: 0

Views: 6934

Answers (2)

John Zwinck
John Zwinck

Reputation: 249552

I turned your code into a self-contained example:

with open('finalplswork.csv', 'wb') as fl:
    writr = csv.writer(fl)
    for subid in range(3):
        submission = 'foo'
        created_utc = 130000
        created_date_utc = 20130704
        data=(subid, created_utc, created_date_utc)
        writr.writerow(data)

It works fine:

$ cat finalplswork.csv 
0,130000,20130704
1,130000,20130704
2,130000,20130704

So your problem is not with the code you've shown here.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124258

Your CSV is created, but you are using a relative file path.

You'd have to print os.getcwd() to know where it is being created, or set the current working directory to where you expect your file to appear.

Best to use an absolute file path instead:

with open('C:/full/path/to/your/documents/folder/finalplswork.csv', 'wb') as fl:

Upvotes: 3

Related Questions