Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatchakorn authored Feb 8, 2021
0 parents commit 0e17d8d
Show file tree
Hide file tree
Showing 5 changed files with 799 additions and 0 deletions.
76 changes: 76 additions & 0 deletions read_xml_from_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import mysql.connector
from mysql.connector import Error
import xml.etree.ElementTree as ET
from xml.dom import minidom


def compute_gpa(score):
if score >= 90: # A+
return 4.5
elif score >= 85: # A
return 4
elif score >= 80: # A-
return 3.7
elif score >= 77: # B+
return 3.3
elif score >= 73: # B
return 3
elif score >= 70: # B-
return 2.7
elif score >= 67: # C+
return 2.5
elif score >= 63: # C
return 2.3
elif score >= 60: # C-
return 2
elif score >= 50: # D
return 1
else:
return 0 # E


try:
# Connect MySQL/test1 database
connection = mysql.connector.connect(
host='localhost', # host name
database='xmldb', # database name
user='tata', # account
password='123123') # password

if connection.is_connected():
# version of database
db_Info = connection.get_server_info()
print("version of database:", db_Info)

# show information of the current database
cursor = connection.cursor()
cursor.execute("SELECT DATABASE();")
record = cursor.fetchone()
print("current database:", record)

cursor = connection.cursor()
query = "SELECT * FROM score_data;"
cursor.execute(query)

root = ET.Element("score_data")

for (student_id, xml_score, data_structure_score, algorithm_score, network_score) in cursor:
sub_1 = ET.SubElement(root, "student", student_id=student_id)
xml_class = ET.SubElement(sub_1, "xml_class", gpa=str(compute_gpa(xml_score))).text = str(xml_score)
xml_class = ET.SubElement(sub_1, "data_structure", gpa=str(compute_gpa(data_structure_score))).text = str(data_structure_score)
xml_class = ET.SubElement(sub_1, "algorithm", gpa=str(compute_gpa(algorithm_score))).text = str(algorithm_score)
xml_class = ET.SubElement(sub_1, "network", gpa=str(compute_gpa(network_score))).text = str(network_score)

# Save the xml file
xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=" ")
with open("./xml/output.xml", "w") as f:
f.write(xmlstr)

except Error as e:
print("connection failure:", e)

finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("database connection closed")
39 changes: 39 additions & 0 deletions seperate_col.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pandas as pd
import glob
import re


def split_to_groups(list_, n_group):
assert (len(list_) / n_group) < 998, "Can't split to {} groups".format(len(l) / n)

if list_ == []:
return []

else:
f = [list_[:n_group]]
f.extend(split_to_groups(list_[n_group:], n_group))
return f


file_path = glob.glob(r"./kluay_csv/*.csv")

for file in file_path:

dict = {}
dict_index = 0
print(file[12:-4])

df = pd.read_csv(file)
num_table = re.findall("\d+\.\d+", str(df["Length"].to_string()))
num_table.pop(-1)

n = split_to_groups(num_table, 3)

for n_1, n_2, n_3 in n:
dict[dict_index] = {"latitude": n_1, "longtitude": n_2, "height": n_3}
dict_index = dict_index + 1

df = pd.DataFrame.from_dict(dict, "index")

path = f'.//kluay_csv//{file[12:-4]}_LLL.csv'
df.to_csv(path)
Loading

0 comments on commit 0e17d8d

Please sign in to comment.