Python CSV String to Array

Python csv string to array

You can convert a string to a file object using io.StringIO and then pass that to the csv module:

from io import StringIO
import csv

scsv = """text,with,Polish,non-Latin,letters
1,2,3,4,5,6
a,b,c,d,e,f
gęś,zółty,wąż,idzie,wąską,dróżką,
"""

f = StringIO(scsv)
reader = csv.reader(f, delimiter=',')
for row in reader:
print('\t'.join(row))

simpler version with split() on newlines:

reader = csv.reader(scsv.split('\n'), delimiter=',')
for row in reader:
print('\t'.join(row))

Or you can simply split() this string into lines using \n as separator, and then split() each line into values, but this way you must be aware of quoting, so using csv module is preferred.

On Python 2 you have to import StringIO as

from StringIO import StringIO

instead.

How to read strings from a csv file and convert it into a integer array?

Using a dict for a lookup and list comprehension

Ex:

check = {'low': 1, "high": 3}

with open('sample.csv') as infile:
csv_file = csv.reader(infile)
next(csv_file) # skip header
result = [[check[c] for c in row] for row in csv_file]

Convert from CSV to array in Python

You should use the csv module:

import csv

results = []
with open("input.csv") as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC) # change contents to floats
for row in reader: # each row is a list
results.append(row)

This gives:

[[0.000264, 0.000352, 8.7e-05, 0.000549], 
[0.00016, 0.000223, 1.1e-05, 0.000142],
[0.008853, 0.006519, 0.002043, 0.009819],
[0.002076, 0.001686, 0.000959, 0.003107],
[0.000599, 0.000133, 0.000113, 0.000466],
[0.002264, 0.001927, 0.00079, 0.003815],
[0.002761, 0.00288, 0.001261, 0.006851],
[0.000723, 0.000617, 0.000794, 0.002189]]

How do I load a .csv file with strings and floats in python?

Use pandas to load your csv file, and then convert it to numpy array using:

import numpy as np
import pandas as pd

df = pd.read_csv('tester.csv')
df_to_array = np.array(df)

Your csv will be stored in df_to_array as a numpy array.

Reading and splitting a .csv file, which contains strings with commas in

You should use the csv module:

import csv

with open('test.csv') as f:
reader = csv.reader(f)

for row in reader:
print(row)

Output:

['1', '2', 'a,b', '3']
['4', 'c,d', '5', '6']

Or, if you don't want to read lines lazily and want all in a single list, as in your question, you can simply do:

with open('test.csv') as f:
reader = csv.reader(f)
data = list(reader)

print(data)
# [['1', '2', 'a,b', '3'], ['4', 'c,d', '5', '6']]

How to import a csv-file into a data array?

Assuming the CSV file is delimited with commas, the simplest way using the csv module in Python 3 would probably be:

import csv

with open('testfile.csv', newline='') as csvfile:
data = list(csv.reader(csvfile))

print(data)

You can specify other delimiters, such as tab characters, by specifying them when creating the csv.reader:

    data = list(csv.reader(csvfile, delimiter='\t'))

For Python 2, use open('testfile.csv', 'rb') to open the file.

Convert String into 1d numpy float array from csv

Just read the CSV normally and then use the built-in function ast.literal_eval to parse the strings into arrays of floats:

import ast
df = pd.read_csv('YOUR FILE.csv', sep=';')
df.loc[:, 'COl1':] = df.loc[:, 'COl1':].apply(lambda col: col.apply(ast.literal_eval).apply(np.asarray))

Output:

>>> df
COL0 COl1 COL2 COL3 COL9999
0 SomeText0 [-3.45, 0.23] [-1.4, 0.21] [-1.35, 0.13] [-1.87, 0.12]
1 SomeText1 [-3.05, 0.2] [-0.4, 0.01] [-0.05, 0.03] [-1.65, 0.33]
2 SomeText2 [-0.4, 0.03] [-1.0, 0.2] [-0.35, 0.03] [-1.43, 0.12]

Convert a numpy array to a CSV string and a CSV string back to a numpy array

First you should use join this way to avoid the last comma issue:

VIstring = ','.join(['%.5f' % num for num in VI])

Then to read it back, use numpy.fromstring:

np.fromstring(VIstring, sep=',')

Converting CSV into Array in Python

You can do this:

with open("Solutions10.csv") as csvfile:
result = [eval(k) for k in csvfile.readlines()]

Edit: Karl is cranky and wants you todo this:

with open("Solutions10.csv") as csvfile:
result = []
for line in csvfile.readlines():
line = line.replace("[","").replace("]","")
result.append([int(k) for k in line.split(",")]

But you're the programmer so you can do what you want. If you trust your input file eval is fine.



Related Topics



Leave a reply



Submit