1

I need to import CSV file into cassandra using python driver

import csv

with open('place_ratings.csv') as f_in:
   companies = csv.load(f_in)

with open('place_ratings.csv') as f_in:
   companies = csv.reader(f_in)

with open('C:\apache-cassandra-3.11.7\MonashMRDB_datasets\place_ratings.csv') as f_in:
   companies = csv.reader(f_in)

I tried everything above, wil r, rt and almost every option

I still get the error:

  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'user_ratings.csv'

what could be done here?

1 Answer 1

4

The issue appears to be the path to the CSV.

Use a forward slash instead which works for both Unix and Windows. For example:

C:/apache-cassandra-3.11.7/MonashMRDB_datasets/place_ratings.csv

The alternative on Windows is to escape the backslash with another backslash:

C:\\apache-cassandra-3.11.7\\MonashMRDB_datasets\\place_ratings.csv

But the best practice is to use os.path so it's guaranteed to work on any platform:

os.path.join("C:", "apache-cassandra-3.11.7\MonashMRDB_datasets", "place_ratings.csv")

As a side note, writing new code for importing CSVs is reinventing the wheel. I recommend you use the free DataStax Bulk Loader (docs, source) tool (dsbulk). DSBulk lets you load/unload data from/to CSV or JSON. Cheers!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.