Ask Your Question
0

Eliminating string after reading

asked 2020-05-06 22:36:16 +0200

Cyrille gravatar image

Suppose I have a file called data.cvs which is

3;2
2;1
1;0.1
0.01;-0.9

I can read it by

import csv
data=list( csv.reader(open('data3.csv','r')) )

but for SM it will had the structure af a matrix of string

[['3;2'], ['2;1'], ['1;0.1'], ['0.01;-0.9']]

How could I eliminate the quotes and replace ; by , ? In passing import is not a word in the documentation .

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-05-06 23:06:33 +0200

tmonteil gravatar image

updated 2020-05-06 23:08:29 +0200

Note that csv means coma separated value. Here, you want a semicolon to be used as a delimiter, so you can do:

sage: data=list( csv.reader(open('data.csv','r'), delimiter=';'))
sage: data
[['3', '2'], ['2', '1'], ['1', '0.1'], ['0.01', '-0.9']]

Note that it is a list of lists of strings, not number. However, the matrix constructor is able to handle this : you can make it a numerical matrix as follows:

sage: matrix(RDF,data)
[ 3.0  2.0]
[ 2.0  1.0]
[ 1.0  0.1]
[0.01 -0.9]
edit flag offensive delete link more
0

answered 2020-05-06 23:05:45 +0200

Emmanuel Charpentier gravatar image

Perusing the doc may give you the answers you seek...

edit flag offensive delete link more

Comments

A nice doc, I never think to look at Python doc when I am in Sagemath.

Cyrille gravatar imageCyrille ( 2020-05-07 07:43:36 +0200 )edit

Wups ! That was meant as a comment, damnit !

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-05-07 19:16:46 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-05-06 22:36:16 +0200

Seen: 338 times

Last updated: May 06 '20