D
D
dan_gi2020-06-15 09:06:26
Python
dan_gi, 2020-06-15 09:06:26

How to create the desired function?

import re

content = 'a:3:{s:6:"update";i:1591804805;s:8:"encoding";s:12:"utf-8";s:4:"urls";a:4:{i:991;a:2:{' \
          'i:0;s:1:"/";i:1;s:66:"<a href="https://site1.com">фильмы онлайн</a>";}i:1113;a:2:{' \
          'i:0;s:1:"/";i:1;s:108:"Смотреть <a href="https://site2.net">кино</a> онлайн без регистрации";}i:1793;a:2:{' \
          'i:0;s:1:"/";i:1;s:149:"Советую фильмы на сайте <a ' \
          'href="http://site3.online">site3.online</a>";}i:1822;a:2:{i:0;s:1:"/";i:1;s:73:"<a ' \
          'href="https://www.site4.org/">ФИЛЬМЫ - ЛУЧШЕЕ</a> ";}}}'

chunks = re.findall(r'i:1;s:.+?:\"(.+?)";}', content)
text = ' | '.join(chunks)

print(text)


This code cleans the links, but how can I make the function take data from the links.db file (it already contains what is now in content) and return the data (instead of print (text), you need to do return text).
In general, the function should take this file as input, process and return data.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dan_gi, 2020-06-15
@dan_gi

import re
def func():
with open ('links.db', 'r', encoding="utf-8") as f:
content = f.read()
chunks = re.findall(r'i:1; s:.+?:\"(.+?)";}', content)
text = ' | '.join(chunks)
return text
print(func())
This was the solution, thanks everyone, I googled a bit and + people helped in the comments)

D
dmshar, 2020-06-15
@dmshar

It is necessary that:
"accepted data from the file as input".
"In principle, it is not necessary for the function to accept something, you need to do something so that it simply opens the file, reads data from the file, and processes it according to that processing.
" you just don't know how to open the file and read the data? And how to create a function? And you don’t even understand what they write about this in any textbook / website / video tutorial? Cool!
And you don’t even have an idea what is in one click in Google? Does this happen today?
And you are absolutely not interested in finding the answer to a school-elementary question on your own, but do you want someone to do it for you? Fantasy! And why then even start to sit down for programming?
Well, I'll help you.Working with files when programming in Python
And more. Python function creation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question