-1

I'm trying to turn a csv file into arrays.testdata here

what I have in csv file is like this

11,10,8,12,13,11 0,1,0,2,3,0 5,15,13,11,18,18

I want to turn it into arrays as below,

[[[11],
  [10],
  [8],
  [12],
  [13],
  [11]],

 [[0],
  [1],
  [0],
  [2],
  [3],
  [0]],

 [[5],
  [15],
  [13],
  [11],
  [18],
  [18]]]
7
  • 5
    Hi and welcome to SO! Please post your attempts in getting the results along with your input and expected output. This helps people understand your question and replicate your problem. Commented Oct 26, 2018 at 1:09
  • Please, If is a csv, share the input to be possible help you. Welcome! Commented Oct 26, 2018 at 1:10
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic, how to ask, and ... the perfect question apply here. StackOverflow is not a design, coding, research, or tutorial resource. However, if you follow whatever resources you find on line, make an honest coding attempt, and run into a problem, you'd have a good example to post. Commented Oct 26, 2018 at 1:12
  • 2
    Possible duplicate of Convert to CSV to array in python Commented Oct 26, 2018 at 1:13
  • You can load the data with numpy.genfromtext and reshape it. But do you really want to get an array? You show a list of lists as desired output. Commented Oct 26, 2018 at 1:28

3 Answers 3

0

Read the file and get the items as list from it:

import csv

results = []

with open('some_array.csv','r') as f:
    lines = csv.reader(f)
    for line in lines:
        results.append([[int(i)] for i in line])

>>results
[[['11'], ['10'], ['8'], ['12'], ['13'], ['11']],
 [['0'], ['1'], ['0'], ['2'], ['3'], ['0']],
 [['5'], ['15'], ['13'], ['11'], ['18'], ['18']]]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Bernard, but I followed your code, I only got the first number from each row. [[['11']], [['0']], [['5']]]
I edited my answer based on your updated input. The earlier file did not have comma delimiters.
Thanks!!! it works. since not all the data in my file are integers, so I changed int(i) into float(i) Thank you!
No problem! Also take note of the other comments highlighting valid concerns of what you might want to output. If any of the answers here helped you remember to check them as answered (under the vote buttons), this helps close questions and help focus on new ones. :)
0
  1. First you can read the file into an array.
  2. After you can use a map function to convert every string list to an integer list

Solution:

import csv
with open('input.csv', 'rb') as f:
    reader = csv.reader(f)
    lines = [row for row in reader]

array = [map(int,l) for l in lines]

array
[[11, 10, 8, 12, 13, 11], [0, 1, 0, 2, 3, 0], [5, 15, 13, 11, 18, 18]]

Comments

0
import csv

output = []

with open('test1.csv', 'r') as f:
    content = csv.reader(f, delimiter=',')

    for line in content:
        clean_line = filter(None, line)  # remove extra spaces
        output.append([[int(i)] for i in clean_line])

>>> print output
[[[11], [10], [8], [12], [13], [11]], [[0], [1], [0], [2], [3], [0]], [[5], [15], [13], [11], [18], [18]]]  

Tested and results match desired output:

desired = [ [[11],[10],[8],[12],[13],[11]], 
            [[0],[1],[0],[2],[3],[0]], 
            [[5],[15],[13],[11],[18],[18]] ]

# print output == desired   # True

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.