Python Forum
How to remove apostrophes in a matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove apostrophes in a matrix
#1
Hi all! I am very near to have a real matrix. Only some apostrophes to remove.
import csv
from pprint import pprint
def matrix():
    pass

with open('numbers.csv', newline='') as csvfile:
    puzzle=csv.reader(csvfile, delimiter=' ', quotechar='|')
    print(puzzle)

    for row in puzzle:#origin
        print(', '.join(row))#originr
        print(row)
matrix=row
print(matrix)
The output is:
Output:
λ python setup.py <_csv.reader object at 0x000001E0CDD1AE80> [0, 0, 9, 0, 0, 0, 5, 6, 2], [0, 0, 0, 9, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 3, 1, 7, 0], [0, 0, 0, 1, 9, 0, 7, 8, 0], [0, 7, 5, 0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 5, 0, 6, 3, 0], [4, 0, 0, 0, 0, 8, 2, 9, 0], [0, 0, 0, 7, 0, 0, 0, 0, 0], [0, 0, 8, 0, 0, 0, 3, 5, 7] ['[0, 0, 9, 0, 0, 0, 5, 6, 2]', '[0, 0, 0, 9, 0, 0, 0, 0, 0]', '[2, 0, 0, 0, 0, 3, 1, 7, 0]', '[0, 0, 0, 1, 9, 0, 7, 8, 0]', '[0, 7, 5, 0, 0, 0, 0, 0, 0]', '[0, 0, 0, 2, 5, 0, 6, 3, 0]', '[4, 0, 0, 0, 0, 8, 2, 9, 0]', '[0, 0, 0, 7, 0, 0, 0, 0, 0]', '[0, 0, 8, 0, 0, 0, 3, 5, 7]'] ['[0, 0, 9, 0, 0, 0, 5, 6, 2]', '[0, 0, 0, 9, 0, 0, 0, 0, 0]', '[2, 0, 0, 0, 0, 3, 1, 7, 0]', '[0, 0, 0, 1, 9, 0, 7, 8, 0]', '[0, 7, 5, 0, 0, 0, 0, 0, 0]', '[0, 0, 0, 2, 5, 0, 6, 3, 0]', '[4, 0, 0, 0, 0, 8, 2, 9, 0]', '[0, 0, 0, 7, 0, 0, 0, 0, 0]', '[0, 0, 8, 0, 0, 0, 3, 5, 7]']
Reply
#2
Your matrix variable only stores the last row of your csv file... and I think is not what you want.
matrix = [row for row in puzzle]
Will read all the rows in a list (so matrix is a list of lists)
Reply
#3
Where do I put your line.
[errorTraceback (most recent call last):
File "setup.py", line 15, in <module>
matrix = [row for row in puzzle]
File "setup.py", line 15, in <listcomp>
matrix = [row for row in puzzle]
ValueError: I/O operation on closed file.][/error]
Reply
#4
With Larz's book I found:
for i in range(9):
matrix[i]=eval(matrix[i])
Reply
#5
What I was suggesting is this:
import csv
 
with open('numbers.csv', newline='') as csvfile:
    puzzle=csv.reader(csvfile, delimiter=' ', quotechar='|')
    # Read all the rows so you get
    # matrix = [[0, 0, 9, 0, 0, 0, 5, 6, 2], [0, 0, 0, 9, 0, 0, 0, 0, 0], ...]
    matrix = [row for row in puzzle]

# If now you want to print the matrix
for row in matrix:
    print(', '.join(row))

# Or transform it to integer matrix and not just strings... assuming all the elements are integers.
matrix =[[int(s) for s in row] for row in matrix]
About using eval... avoid it as much as you can. It is useful and powerful, specially when you are a noob but almost anything you can do with eval you can do it better in another way without the risk of explosion (not to mention the huge security problem)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,300 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Remove isolated vertices from dictionary and adjacency matrix Weird 1 1,805 Jan-18-2020, 04:33 PM
Last Post: Weird
  remove apostrophes in list sparkz_alot 5 9,952 Jul-21-2017, 09:21 PM
Last Post: sparkz_alot
  Is it possible to avoid apostrophes ? sylas 5 3,770 Jul-19-2017, 11:51 AM
Last Post: Larz60+
  matrix from matrix python numpy array shei7141 1 3,642 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020