-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]]]
Grace Li
  • 19
  • 3
  • 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. – BernardL Oct 26 '18 at 01:09
  • Please, If is a csv, share the input to be possible help you. Welcome! – Andre Araujo Oct 26 '18 at 01: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](http://stackoverflow.com/help/on-topic), [how to ask](http://stackoverflow.com/help/how-to-ask), and [... the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-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. – Prune Oct 26 '18 at 01:12
  • 2
    Possible duplicate of [Convert to CSV to array in python](https://stackoverflow.com/questions/37173892/convert-to-csv-to-array-in-python) – jtweeder Oct 26 '18 at 01:13
  • You can load the data with [numpy.genfromtext](https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html) and [reshape it](https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html#numpy.reshape). But do you really want to get an array? You show a list of lists as desired output. – Mr. T Oct 26 '18 at 01:28
  • `np.loadtxt(your_file)` should also work – Rocky Li Oct 26 '18 at 01:28
  • please double check that expected array output it's not valid – chickity china chinese chicken Oct 26 '18 at 01:31

3 Answers3

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']]]
BernardL
  • 5,162
  • 7
  • 28
  • 47
  • Thanks Bernard, but I followed your code, I only got the first number from each row. [[['11']], [['0']], [['5']]] – Grace Li Oct 26 '18 at 01:35
  • I edited my answer based on your updated input. The earlier file did not have comma delimiters. – BernardL Oct 26 '18 at 01:40
  • Thanks!!! it works. since not all the data in my file are integers, so I changed int(i) into float(i) Thank you! – Grace Li Oct 26 '18 at 02:00
  • 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. :) – BernardL Oct 26 '18 at 02:05
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]]
Andre Araujo
  • 2,348
  • 2
  • 27
  • 41
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