A
A
Alexander2020-12-09 16:40:02
Python
Alexander, 2020-12-09 16:40:02

How to delete the last 4 rows in a matrix?

I must say right away that pandas cannot be used. There is a code, I need to make a function in module_3 that deletes the last 4 lines in the matrix (which is in the file), please help

import csv
from matplotlib.pyplot import matshow, show
from module_1 import task_1
from module_2 import task_2
from module_3 import task_3
def read(csvfile):
    with open(csvfile, 'r') as file:
        r = list(csv.reader(file))
    for x in range(len(r)):
        for y in range(len(r[x])):
            r[x][y] = int(r[x][y])
    return r
def write(matrix, name = "result.csv"):
    with open(name, 'w') as file:
        for x in range(len(matrix)):
            for y in range(len(matrix[x])):
                if y == len(matrix[x]) - 1:
                    file.write(str(matrix[x][y]) + "\n")
                else:
                    file.write(str(matrix[x][y]) + ",")
t1 = task_3(read("tekst.csv"))
for x in range(len(t1)):
    print(t1[x], end=' ')
print('\n')
t2 = task_2(read("tekst.csv"))
for x in range(len(t2)):
    for y in range(len(t2[x])):
        print(t2[x][y], end=' ')
    print()
t3 = task_1(read("tekst.csv"))
for x in range(len(t3)):
    print(t3)
write(t2)
matshow(t2)


Here is module_3 (this code doesn't work, I don't understand how to delete lines)

def task_3(m):
    m[-int(4) + m]
    return m

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nvlveu, 2020-12-09
@nvlveu

def task_3(m):
    m_lines = m.split("\n")[:-4]
    m = ""
    for line in m_lines:
        m += f"{line}\n"
    return m[:-1]

A
Andrey Dugin, 2020-12-09
@adugin

Who is the search bar for?
How to remove last rows in matrix from csv file?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question