Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
- # rgb2pickle.py
- import pickle
- myList = []
- myDict = {}
- def read_file_textlines(filname):
- """read text from file and return the file content in a list of lines"""
- import io
- signal = None
- try:
- with open(filname, "r") as f:
- text_lines = f.read().splitlines()
- signal = True # Done.
- return signal, text_lines
- except IOError:
- signal = False # Something went wrong !!
- return signal, ''
- # ---txt tool section------------------------------------------------------ #
- def emptystring(txt):
- if txt == '': # txt == '0'
- txt = '00'
- return txt
- def onelychrs(txt):
- if len(txt) < 2:
- txt = f"0{txt}"
- return txt
- def chkzero(txt):
- if txt == '0':
- txt = '00'
- return txt
- def dec2hex(txt):
- txt = hex(int(txt)).lstrip("0x")
- txt = txt.upper()
- txt = onelychrs(txt)
- txt = chkzero(txt)
- return txt
- def txt2dict(myList):
- i = 0
- aDict = {}
- for item in myList:
- newList = []
- # pick name and clean it for tab
- name = item[13:].lstrip("\t")
- name = name.rstrip()
- # pick RGB values (dec)
- R = item[0:3].lstrip()
- R = emptystring(R)
- G = item[4:7].lstrip()
- G = emptystring(G)
- B = item[8:11].lstrip()
- B = emptystring(B)
- # convert dec2hex value
- H1 = dec2hex(R)
- H2 = dec2hex(G)
- H3 = dec2hex(B)
- # build a hash value
- H = f"#{H1}{H2}{H3}"
- # make a list with a new column structure
- newList.append(name)
- newList.append(R)
- newList.append(G)
- newList.append(B)
- newList.append(H)
- aDict[i] = newList
- i += 1
- return aDict
- # Main code starts here...
- signal, myList = read_file_textlines('rgb.txt')
- print(f'Signal is: {signal}')
- myDict = txt2dict(myList)
- fn = 'list.pkl'
- outfile = open(fn, 'wb')
- pickle.dump(myDict, outfile)
- outfile.close()
- print('Finished!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement