How to find pattern in text with python re module - if i have text like this. Data in…
- User
How to find pattern in text with python re module - if i have text like this. Data in round brackets is not literal it is used to tell you which text is used. Everything not in brackests should be taken literally data betven %% signs should be returned as a result. Do not take % signs literally, I am marking what i need to extract with them:
'[[(predefined str i need to match)]] (some text, symbols, numbers) Защищённый прикреплённый файл: medcard/attached_file/%(4 numbers)-(2 numbers)-(2 numbers)%/(Filename wich can consist of any letter big or small, numbers or symbols).(file extention)\n Дата: %(2 numbers) (month name - letters)%\nИмя обследования: %(phrase of several words, can have numbers or symbols)%\n (some data, symbols, numbers, spaces etc) "dt_created":"%(date in such format 2021-04-24T12:48:30+03:00)%"'
- Assistant
- User
rhat means .? re
- Assistant
In regular expressions, the period (
.) is a special character that matches any single character except a newline. When used in conjunction with other quantifiers like*,+, or?, it can match multiple characters.Here's a brief explanation of how works with quantifiers:
- User
how to unpack list of tuples: [(1,2,3),(1,2,3)] -> 1 2 3 1 2 3 short answer
- Assistant
You can unpack a list of tuples into individual elements using Python's
itertools.chaincombined with the*operator. Here is the concise way to achieve that:pythonfrom itertools import chain lst_of_tuples = [(1,2,3),(1,2,3)] result = list(chain(*lst_of_tuples)) print(result) # Output: [1, 2, 3, 1, 2, 3]If you just want to print the elements without including them in a list, you can do:
pythonprint(*chain(*lst_of_tuples)) # Output: 1 2 3 1 2 3This uses
chainto flatten the list of tuples, and the*operator to unpack the items. - User
how to write lines to xlsx or xls file using python
- Assistant
To write lines to an XLSX or XLS file using Python, you can use different libraries depending on the format you need. For XLSX files,
openpyxlorxlsxwriterare popular libraries, and for XLS files,xlwtis commonly used.Below are examples of how to write lines to both XLSX and XLS files:
- User
i need to write list of lines of a 2d list using xlwt - how to do it: [[1, 2, 3], [1, 2, 3]] -> I need to get xls file with 2 lies with 3 elements in each line short answer - dont explain
- Assistant
Sure, here is the concise code to achieve that using
xlwt:pythonimport xlwt # Create a workbook and a worksheet workbook = xlwt.Workbook() worksheet = workbook.add_sheet('Sheet1') # 2D list to be written lines = [[1, 2, 3], [1, 2, 3]] # Write lines to the worksheet for row_num, row_data in enumerate(lines): for col_num, cell_data in enumerate(row_data): worksheet.write(row_num, col_num, cell_data) # Save the workbook workbook.save('output.xls')