cls_str
import re
def readlines(filename):
"""Read all the lines in a text file and return as a list
"""
with open(filename, 'r') as f:
lines = f.read().splitlines()
return lines
def writedict(dict_data):
with open('test_data.txt', 'w') as f:
for i, key in enumerate(dict_data):
if i == 0:
f.writelines("".ljust(12))
for sub_key in dict_data[key]:
sub_key = sub_key+" "
f.writelines(sub_key.ljust(10))
f.write('\n')
new_key = str(key)[:4] + ': '
f.writelines(new_key.ljust(10))
for sub_key in dict_data[key]:
print(sub_key)
f.writelines(str(dict_data[key][sub_key]))
f.write('\n')
if __name__=="__main__":
lines = readlines("cls_str.txt")
company = {}
print("len:{}".format(len(lines)))
sub_item = {}
pre_li = None
id = 0
for i, li in enumerate(lines):
if "市值:" in li:
sz = {"市值": li.split(":")[1]}
sub_item.update(sz)
if "营收:" in li:
ys = {"营收": li.split(":")[1]}
sub_item.update(ys)
if "净利润:" in li:
jlr = {"净利润": li.split(":")[1]}
sub_item.update(jlr)
if "研发投入:" in li:
yftr = {"研发投入": li.split(":")[1]}
sub_item.update(yftr)
if "研发人员:" in li:
yfry = {"研发人员": li.split(":")[1]}
sub_item.update(yfry)
if "员工数量:" in li:
ygsl = {"员工数量": li.split(":")[1]}
sub_item.update(ygsl)
pattern = re.compile(r"\d+、")
result = re.findall(pattern, li)
if result:
id = int(result[0][:-1])
if id ==1:
pre_li = li.split("、")[1]
if id > 1:
item = {pre_li: sub_item}
pre_li = li.split("、")[1]
company.update(item)
sub_item = {}
item = {pre_li: sub_item}
company.update(item)
print(company)
writedict(company)