# 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 help

edit retag close merge delete

Sort by ยป oldest newest most voted

If 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])

more

Hi,

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.

more

Thanks that works a treat.

more

Welcome 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!

( 2013-05-20 09:18:40 -0500 )edit

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

## Stats

Asked: 2013-05-20 03:17:16 -0500

Seen: 620 times

Last updated: May 20 '13