A
A
Alexander2020-12-08 16:55:08
Python
Alexander, 2020-12-08 16:55:08

How to remove last rows in matrix from csv file?

There are three codes, one is the main one, which executes the others, the codes read the matrix from one csv file, delete the last 4 lines and write a new matrix (without the last 4 lines) to a new csv file. Tell me how to do so that the lines are deleted
Here is the main code:

import matrixfile as mf
import matrixziro as mz
from matplotlib.pyplot import matshow, show

mzr = mz.GetZiroMatrix(mf.GetMatrix('tekst.csv')) 
mf.SaveMatrix(mzr, 'resultat_2.csv')

matshow(mzr)
show()

The second file is called matrixziro.py, it fails to make a replacement
def GetZiroMatrix(mt):
    return [len(m) - 1], m[0] = m[0],len(m) - 1]* len(row) for row in mt] #не работающая функция,оператор должен остаться обязательно

third file matrixfile.py
import csv

def GetMatrix(filename):
    return [[int(token) for token in line.split()] for line in open(filename)]

def SaveMatrix(mt, filename):
    with open(filename, 'w') as f:
        for row in mt:
            print(' '.join(map(repr, row)), file=f)

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Andrey Dugin, 2020-12-08
@adugin

Why are you inventing bicycles?

with open('input.csv', 'r') as fi, open('output.csv', 'w') as fo:
    print(*fi.readlines()[:-4], file=fo, sep='')

It is possible (and even necessary!) via pandas:
import pandas a pd

pd.read_csv(..., skipfooter=4).to_csv(...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question