Commit fe1a2a98 authored by redmood's avatar redmood

saved words in xml

parent 45b1f0e4
#-*- coding: utf-8 -*-
import csv
import json
class Word:
def __init__(self,model,pk,french,morwuwhw,precisions):
self.french = french
self.morwuwhw = morwuwhw
self.precisions = precisions
self.pk = pk
self.model = model
def to_json(self):
return {"model": self.model,
"pk": self.pk,
"fields": {
"french": self.french,
"morwuwhw": self.morwuwhw,
"precisions": self.precisions}}
class Words:
MODELS = "dictionnary.word"
def __init__(self,csv_file_path):
self.words = []
nb_word = 1
with open(csv_file_path) as f:
reader = csv.reader(f,delimiter=';',quotechar="'")
for row in reader:
print(row)
new_word = Word(self.MODELS,nb_word,row[0],row[1],row[2])
print(new_word.to_json())
nb_word = nb_word + 1
self.words.append(new_word)
def to_json(self):
return [word.to_json() for word in self.words]
def json_dump(self,file_name):
with open(file_name, 'w') as outfile:
json.dump(self.to_json(),outfile,indent=4,sort_keys=True,ensure_ascii=False)
words = Words("saved_data/words-data.csv")
words.json_dump("saved_data/words-data.json")
#with open(path) as f:
# reader = csv.reader(f)
# for row in reader:
# log.debug("french: {}, morwuwhw: {}, precisions: {}".format(row[0],row[1],row[2]))
# _, created = Word.objects.get_or_create(
# french=row[0],
# morwuwhw=row[1],
# precisions=row[2],
# )
# creates a tuple of the new object or
# current object and a boolean of if it was created
#-*- coding: utf-8 -*-
from morwuwhw_dict.alt_settings.test import *
import psycopg2
import logging as log
import csv
log.basicConfig(level=log.DEBUG)
try:
log.debug("trying to connect database with: dbname='{}' user='{}' password='{}'".format(
DATABASES['default']['NAME'],
DATABASES['default']['USER'],
DATABASES['default']['PASSWORD'],
))
conn = psycopg2.connect("dbname='{}' user='{}' password='{}'".format(
DATABASES['default']['NAME'],
DATABASES['default']['USER'],
DATABASES['default']['PASSWORD'],
))
log.info("connected to the database")
except:
log.debug("unable to connect to the database")
cur = conn.cursor()
table = "dictionnary_word"
sort_attribute = "french"
filename = "saved_data/words-data.csv"
try:
cur.execute("SELECT * FROM {} ORDER BY {}.{}".format(table, table, sort_attribute))
with open(filename,mode='w') as data_file:
writer = csv.writer(data_file,delimiter=';')
item = cur.fetchone()
while item != None:
writer.writerow(["'{}'".format(item[2]), "'{}'".format(item[1]), "'{}'".format(item[3])])
item = cur.fetchone()
except TypeError:
log.debug("failed to write data in file {}".format(filename))
except:
log.debug("failed to fetch data from table {}".format(table))
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment