csv.py

Created by antarctus

Created on January 15, 2022

2.26 KB

Utilisez l’encodage CSV !!
Apprenez l’utilisation de base de données avec l’encodage Comma-Separated Values.
étapes :
1 ) Mettez votre fichier dans une variable Python avec la virgule comme séparateur des colonnes et le retour à la ligne comme séparateur des lignes.
2 ) Exécutez votre_variable=Cvs(la_variable_de_lencodage)
3) Utiliser une boucle for i in votre_variable: ... avec i comme tuple avec un élément par colonne !


Voici la documentation :
https://numworks.antarctus.repl.co/documentation/csv.html


try:
  from os import *
  omega=True
except:
  omega=False

class Csv(object):
  def __init__(self,start=""):
    self.fichier=None
    if type(start)==str:
      self.load(start)
    elif type(start)==list:
      self.lines=start


  def create(self,name,ext="csv"):
    self.fichier=open(name+"."+ext,"w")
    self.fichier.write(self.data())
    self.fichier.close()

  def load(self,data):
    self.data_lines=data.split("\n")
    while "" in self.data_lines:
      self.data_lines.remove("")
    self.lines=[]
    for i in self.data_lines:
      if not '"' in i:
        self.lines+=[i.split(","),]
      else:
        self.lines+=[i.split(','),]
        for j in range(len(self.lines[-1])):
          if j==len(self.lines[-1]):
            break
          if '"'==self.lines[-1][j][0]:
            while self.lines[-1][j][-1]!='"':
              self.lines[-1][j]=self.lines[-1][j]+","+self.lines[-1][j+1]
              del self.lines[-1][j+1]
            self.lines[-1][j]=self.lines[-1][j][1:-1]

  def open(self,name):
    if "." in name:
      self.fichier=open(name)
    else:
      self.fichier=open(name+".csv")
    if ".py" in name:
      data=self.fichier.read()[1:-1]
    else:
      data=self.fichier.read()
    self.fichier.close()
    self.load(data)

  def __iter__(self):
    for i in self.lines:
      yield tuple(i)

  def __bool__(self):
    return bool(self.data())

  def __str__(self):
    return self.data()

  def __repr__(self):
    if self.fichier:
      return "<CVS reader of \""+self.fichier.name+"\">"
    return "<CVS reader>"

  def __getitem__(self,i):
    return tuple(self.lines[i])

  def __setitem__(self,i,j):
    self.lines[i]=j

  def __delitem__(self,i):
    del self.lines[i]

  def __add__(self,i):
    return Cvs(self.data()+"\n"+i.data())

  def data(self):
    def call(elem):
      elem=elem[:]
      for i in range(len(elem)):
        elem[i]=str(elem[i])
        if "," in elem[i]:
          elem[i]="\""+elem[i]+"\""
      return ",".join(elem)
    return "\n".join(list(map(call,self.lines[:])))

  def query(self,callback=lambda x: True,key=None):
    r=[]
    for i in self:
      if callback(i):
        r+=[i,]
    if key:
      r.sort(key=key)
    return r

if not omega:
  #Pour tout les pauvres deconnectes
  del Csv.create
  del Csv.open

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.