-
Notifications
You must be signed in to change notification settings - Fork 0
/
how_to_use_example.py
137 lines (107 loc) · 3.35 KB
/
how_to_use_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This file content examples how to use this module
is for saving and loading many file formats
Terminology
otevrit is for open
dump is for save content to file
"""
# for import whole module
import unifile
# or you can import only what you need
from unifile.otevrit import yml as ymlo
from unifile.dump import yml as ymls
import os
# write and save data to yaml
print("################################\nYaml:\n################################")
# your settings what you need save
settings = {
"network": {
"port": 22
},
"more_settings": True
}
# saving settings
unifile.dump.yml("file.yaml", settings)
# load settings
b = unifile.otevrit.yml("file.yaml")
# print settings
print(b)
# load settings to your program an you ca use later
port = b["network"]["port"]
more_settings = b["more_settings"]
# prints vars
print("Port: " + str(port))
print("more settings " + str(more_settings))
# load and save text to txt file
print("################################\nFile:\n################################")
# \ is for escaping first blank line
my_text = """\
My text
lorem ipsum
"""
# save text
unifile.dump.txt("file.txt", my_text)
# and load and print
print(unifile.otevrit.txt("file.txt"))
# Load data from csv more at: https://en.wikipedia.org/wiki/Comma-separated_values
print("################################\nCSV:\n################################")
# load in array mode
"""
os.path.join() is func for multiplatform path
https://www.geeksforgeeks.org/python-os-path-join-method/
https://docs.python.org/3/library/os.path.html
"""
c = unifile.otevrit.excel(os.path.join("sample", "Import_User_Sample_en.csv"))
print(c)
# Load as str
print("\n# str mode\n")
aa = unifile.otevrit.excel(os.path.join(
"sample", "Import_User_Sample_en.csv"), "str")
print(aa)
print("\n# str mode with delimeter\n")
ii = unifile.otevrit.excel(os.path.join(
"sample", "Import_User_Sample_en.csv"), "str", deli="|")
print(ii)
print("\n# panda mode\n")
uu = unifile.otevrit.excel(os.path.join(
"sample", "Import_User_Sample_en.csv"), "panda")
print(uu)
# save to csv
# data to save
my_list = [
["jana", "female", "15"],
["adam", "male", "85"],
["novak", "male", "57"]
]
list_of_column_names = ["name", "sex", "age"]
# save data with column names
unifile.dump.excel("file.csv", my_list, list_of_column_names)
# save to excel
my_list = [
["jana", "female", "15"],
["adam", "male", "85"],
["novak", "male", "57"]
]
list_of_column_names = ["name", "sex", "age"]
row_names = ['row 1', 'row 2', 'row 3']
# save data with column names
unifile.dump.excel("file.xlsx", my_list, list_of_column_names,
"excel", row_names=row_names)
# savedata json or load from url
print("################################\nJson:\n################################")
# json example
urlData = "http://vocab.nic.in/rest.php/states/json"
jsonData = unifile.otevrit.js(urlData)
# print the state id and state name corresponding
for i in jsonData["states"]:
print(
f'State Name: {i["state"]["state_name"]} , State ID : {i["state"]["state_id"]}')
# json loacal file save
d = {"a": "adsd", "time": {"adaa": "yhsyhs"}}
unifile.dump.js("file.json", d)
# PDF
print("################################\nPdf:\n################################")
# load text from pdf file
print(unifile.otevrit.pdf(os.path.join("sample", "sample.pdf")))