0

I am confused on how to go about parsing an array list from a .csv file. The .csv file gives information on a daily basis, in this format:

{
       "a" : 1,
       "b" : 3,
       "d" : 10
},

The open bracket shows the new day of data, and the closing bracket followed by the comma ends the data (I have no way of changing how the .csv is generated). There are about 400 days worth of data, and each day has the same list items (a,b, and d). How would I go about parsing the .csv data into a readable list format in python? I would post example code, but I have no idea where to even start with this.

Thanks in advance!

4
  • 1
    That looks like JSON to me... Commented Feb 19, 2014 at 4:34
  • Tyler, it very well could be, I am not sure on how the data is exported. I am using python to interpret the .csv that is generated in that format. Commented Feb 19, 2014 at 4:35
  • Are there commas between the a and b fields (e. g. is it {"a" : 1, "b": 3 or {"a" : 1 "b" : 3 as in your example)? Commented Feb 19, 2014 at 4:39
  • Sean, there are commas between the fields. I have updated the original question appropriately. Commented Feb 19, 2014 at 4:40

2 Answers 2

2

Your CSV file is almost certainly JSON. If it is, then Python has a json library you can import and use:

import json

with open('/path/to/your/file.csv', 'r') as file
    data = json.load(file)
    # do things with data here
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was able to import it correctly following your code! A simple json.dumps(data) allows for the customization of arrays (via numpy) for those interested.
1

for parsing csv

import csv
a = ["1,2,3","4,5,6"]   # or a = "1,2,3\n4,5,6".split('\n')     
x = csv.reader(a)

print(list(x))
>>> [['1', '2', '3'], ['4', '5', '6']]

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.