0

I have this csv file that looks a bit like this:

1,2,3
3,4,5
5,6,7

and I need to convert it (preferably in python) to a .js file array that looks more like this:

var variable1 = [
[1,2,3],
[3,4,5],
[5,6,7],
];

I'm very new to programming, so any guidance would be very helpful! Thanks so much!

1 Answer 1

1

You have to open your csv file, then read the lines and write them in your js file.

Basically:
-> Open csv file
-> Create js file
-> Write 'var variable1 = [' in your js file
-> Iterate on csv lines
-> Write them in your js file
-> Write '];' in your js file
-> Close the files

In python:

myJSFile = open('Path_to_your_js_file', 'w')
myJSFile.write("var variable1 = [\n")

myCSVFile = open('Path_to_your_csv_file', 'r')
for line in myCSVFile .readlines() :
    myJSFile.write("[%s],\n" % line.strip())

myJSFile.write("];")

myJSFile.close()
myCSVFile.close()

That should do the work ;)

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.