ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 20 May 2013 16:18:44 +0200import csv file: integers appearing as floating pointshttps://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/Hi,
I am trying to import a csv file in order to create an adjacency matrix and then plot the associated directed graph. I upload the csv file and use its content to generate a matrix as follows
sage: import csv
sage: data = list(csv.reader(file(DATA+'matrix23.csv')))
sage: m = matrix([[ float(_) for _ in line] for line in data])
sage: m
Whereas my csv file only contains integers 1's and 0's, the generated matrix is floating point 1.0's and 0.0's!
Note: the CSV file was created using MS Excel with cells formated to zero decimal places. I'm new to both python and sage and can't find anything on the forums to helpMon, 20 May 2013 10:17:16 +0200https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/Answer by GingerAndy for <p>Hi,</p>
<p>I am trying to import a csv file in order to create an adjacency matrix and then plot the associated directed graph. I upload the csv file and use its content to generate a matrix as follows</p>
<pre><code>sage: import csv
sage: data = list(csv.reader(file(DATA+'matrix23.csv')))
sage: m = matrix([[ float(_) for _ in line] for line in data])
sage: m
</code></pre>
<p>Whereas my csv file only contains integers 1's and 0's, the generated matrix is floating point 1.0's and 0.0's! </p>
<p>Note: the CSV file was created using MS Excel with cells formated to zero decimal places. I'm new to both python and sage and can't find anything on the forums to help</p>
https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?answer=14949#post-id-14949Thanks that works a treat.Mon, 20 May 2013 12:57:43 +0200https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?answer=14949#post-id-14949Comment by vdelecroix for <p>Thanks that works a treat.</p>
https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?comment=17666#post-id-17666Welcome to Sage! If you are pleased with Thierry answer then you should add it as a selected answer. Moreover this message to Thierry answer might be a comment within its answer (answer are somewhat reserved to *answer* the question and comments to *comment* about the questions or the answers). The last but not the least: you may select Thierry's answer as your selected answer in order to close this post!Mon, 20 May 2013 16:18:40 +0200https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?comment=17666#post-id-17666Answer by tmonteil for <p>Hi,</p>
<p>I am trying to import a csv file in order to create an adjacency matrix and then plot the associated directed graph. I upload the csv file and use its content to generate a matrix as follows</p>
<pre><code>sage: import csv
sage: data = list(csv.reader(file(DATA+'matrix23.csv')))
sage: m = matrix([[ float(_) for _ in line] for line in data])
sage: m
</code></pre>
<p>Whereas my csv file only contains integers 1's and 0's, the generated matrix is floating point 1.0's and 0.0's! </p>
<p>Note: the CSV file was created using MS Excel with cells formated to zero decimal places. I'm new to both python and sage and can't find anything on the forums to help</p>
https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?answer=14948#post-id-14948If your matrix `m` only contains entries like `0.O` and `1.0`, you can try to transform it back to an integer matrix:
sage: n = m.change_ring(ZZ)
You can also transform your entries into integers while creating your matrix:
sage: m = matrix([[ ZZ(_) for _ in line] for line in data])
Mon, 20 May 2013 11:25:42 +0200https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?answer=14948#post-id-14948Answer by vdelecroix for <p>Hi,</p>
<p>I am trying to import a csv file in order to create an adjacency matrix and then plot the associated directed graph. I upload the csv file and use its content to generate a matrix as follows</p>
<pre><code>sage: import csv
sage: data = list(csv.reader(file(DATA+'matrix23.csv')))
sage: m = matrix([[ float(_) for _ in line] for line in data])
sage: m
</code></pre>
<p>Whereas my csv file only contains integers 1's and 0's, the generated matrix is floating point 1.0's and 0.0's! </p>
<p>Note: the CSV file was created using MS Excel with cells formated to zero decimal places. I'm new to both python and sage and can't find anything on the forums to help</p>
https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?answer=14950#post-id-14950Hi,
Note that in your code you use "float" to convert a string to a number. But by its very definition this function builds a floating point.Mon, 20 May 2013 16:18:44 +0200https://ask.sagemath.org/question/10141/import-csv-file-integers-appearing-as-floating-points/?answer=14950#post-id-14950