def read_dir(file_path): """ :param file_path: 要读取的文件夹路径 :return: """ if file_path == '' or file_path is None: print('文件路径不能为空') return try: L = [] if not os.path.isfile(file_path): files = os.listdir(file_path) for file in files: L.append(read_file(file_path, file)) else: L.append(read_file(file_path)) return L except Exception as error: print("出现如下异常%s" % error)
# CreateDate:2019/10/30 # FileName:FindWords # IDE:PyCharm # 敏感词文本文件 TestObject.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。 # 北京 # 程序员 # 公务员 # 领导 # 牛比 # 牛逼 # 你娘 # 你妈 # love # sex # jiangge from smallTopics.ReadFileUtil import read_file from smallTopics.sensitiveWords.ValidateWords import validate_word if __name__ == '__main__': list_words = read_file('d:\\TestObject.txt') word = input('请输入词汇:') flag = validate_word(word, list_words) if flag: print('Freedom') else: print('Human Rights')
ws.cell(column=2, row=data_key + 1, value=rows) elif isinstance(lines, list): for row in range(len(lines)): ws.append(lines[row]) wb.save(filename=path) def validate_file_is_work_book(path): dir = path.split('.') if len(dir) >= 2: file_type = dir[len(dir) - 1] if file_type == 'xlsx': return True return False if __name__ == '__main__': read_path = os.path.join('pri', 'numbers.txt') read_lines = read_file(read_path) new_lines = [] for line in read_lines: line = line.rstrip() new_lines.append(line) new_lines = ''.join(new_lines) lines = json.loads(new_lines) txt_change_work_book(lines, os.path.join('excel', 'numbers.xlsx'))
def create_xml(datas, description, path, name): root = ET.Element('root') root.tail = '\n' students = ET.SubElement(root, 'students') students.tail = '\n' str1 = description students.text = str1 + '\n' + str(datas) tree = ET.ElementTree(root) save_path = os.path.join(path, name) tree.write(save_path, encoding="utf-8", xml_declaration=True) if __name__ == '__main__': read_path = os.path.join( 'D:\\Users\\panta\\PycharmProjects\\demo\\openpyxl\\pri', 'student.txt') datas = read_file(read_path) description = ''' <!-- 学生信息表 "id" : [名字, 数学, 语文, 英文] --> ''' create_xml(datas, description, 'xml', 'student.xml')