How to convert a Python csv string to array?



In this article, we are going to find out how to convert a CSV string into an array.

The first approach is by using the split() method. This method takes a parameter, and delimiters the character at which the string should be split. Since the input is a csv, which is comma-separated we will be setting the delimiter as comma and split the string into array.

The split() method in Python is a string manipulation function that divides a larger string into multiple smaller strings. The strings are returned as a list by the split() method.

Example

In the example given below, we are taking a CSV string as input and we are converting it into an array 

str1 = "Hello,Everyone,Welcome,to,Tutorialspoint"

print("The given CSV string is")
print(str1)

print("Converting the given CSV string into array")
res = list(map(str.strip, str1.split(',')))
print(res)

Output

The output of the above example is as follows −

The given CSV string is
Hello,Everyone,Welcome,to,Tutorialspoint
Converting the given CSV string into array
['Hello', 'Everyone', 'Welcome', 'to', 'Tutorialspoint']

Using inner.split() and splitlines()

The second appch is by using inner.split() and splitlines(). This approach is used if the input CSV string is multi-line. In this case, first we should split the lines so we will use splitlines() method for splitting at new line character. After splitting at new line, we should split each line at comma using the inner.split() method, then we should combine them into a single list.

Example

In the example given below, we are taking a multi-line CSV string as input and we are converting it into array using inner.split() and splitlines() 

s = "1, John Doe, Boston, USA\n2, Jane Doe, Chicago, USA"

print("The given CSV string is")
print(s)

print("Converting the given CSV string into array")
res = list(map(str.strip, s_inner.split(',')) for s_inner in s.splitlines())
print((res))

Output

The output of the above example is as follows −

The given CSV string is
1, John Doe, Boston, USA
2, Jane Doe, Chicago, USA
Converting the given CSV string into array
[<map object at 0x7f8074f002b0>, <map object at 0x7f8074f00390>]

Using reader() method of csv library

The third approach is by using the reader() method of csv library. This method takes a CSV string as input and converts it into an array. This method can also be used for multi−line outputs but we have to convert the multi-line using splitlines() method.

Example

In the example given below, we are taking a CSV string as input and we are converting it into array using reader() method 

import csv
str1 = "1, John Doe, Boston, USA\n2, Jane Doe, Chicago, USA".splitlines()

print("The given CSV string is")
print(str1)

print("Converting the given CSV string into array")
x = csv.reader(str1)
print(list(x))

Output

The output of the above example is given below −

The given CSV string is
['1, John Doe, Boston, USA', '2, Jane Doe, Chicago, USA']
Converting the given CSV string into array
[['1', ' John Doe', ' Boston', ' USA'], ['2', ' Jane Doe', ' Chicago', ' USA']]

Advertisements