How to import regex in python - python

Is there any module named regex to use like : regex.compile(pattern, string)
i tried to import it like this: import re, regex and I found this error ModuleNotFoundError: No module named 'regex'

you need to import the python regex library :
https://docs.python.org/3/library/re.html
by doing: import re and then you can put any name you want like regex
such as import re as regex

import re
pattern=re.compile('apple')
result=pattern.findall('apple Dipendra apple')
print(result)
You cannot import packages the way you are importing. I guess your work can be done with the python inbuilt "re" package. Please take a look into the above code snippet by me for your reference

Related

Is it possible to import all packages from a file like we do with requirements.txt?

I am always typing a lot of:
import pandas as pd
import stuff from stuff
import etc...
Is there any way to make a list of imports in a file to use with one line code?
Thanks
Python follows the rule "explicit is better than implicit".
So it's intended to have all imports used at the beginning of your file, even there are many.
If there is too much, it can be a sign that you should split your file into multiple files belonging to the same package.
If you have a list of packages you want to import, saved as my_list.txt, you can do something like this:
with open('my_list.txt', 'r') as packages:
for line in lines:
__import__(line)
As a result, you will import every package indicated by my_list's lines.

Failing to import a file

I am currently trying to code a searching program, which makes use of a program I've already written. It refuses to get to the second print statement.
print("Relevance: ")
# import sqlite3
import Breakdown.py as bd
import re, nltk
from nltk.corpus import wordnet
# from sqlite3 import Error
from autocorrect import spell
print("Input line: ")
The file structure looks like this:
However, I can't work out why it can't get past that import section.
This is somewhat important.
Thanks.
Just write:
import Breakdown as bd
python will import the Breakdown.py file as a module. It will be looking for any variable or function named "py" in the Breakdown module if you use:
import Breakdown.py as bd
... which I don't think is the case here.
You should put the Breakdown.py file in the path where you're starting Python or in one of the directories where Python looks for libraries:
import os
for p in sys.path:
print(p)
and use import Breakdown (no .py).
Or else add to sys.paththe folder where the module is with:
sys.path.append('/your/foldername')

How to fix pattern, that I use to get list of files in folder with standard library glob?

I have the following files:
/tmp/test_glob/client.log.71.gz
/tmp/test_glob/client.log.63.gz
/tmp/test_glob/client.log.11
/tmp/test_glob/core_dump.log
/tmp/test_glob/client.log.32
/tmp/test_glob/dm.log
/tmp/test_glob/client.log
/tmp/test_glob/client.log.1
/tmp/test_glob/client.log.64.gz
I want to get all .log files, EXCEPT the files, that end with .gz.
The desired result should be the following:
/tmp/test_glob/client.log.11
/tmp/test_glob/core_dump.log
/tmp/test_glob/client.log.32
/tmp/test_glob/dm.log
/tmp/test_glob/client.log
/tmp/test_glob/client.log.1
I have written this simple code:
import glob
import os
glob_pattern = u'*.log*'
for log_path in glob.glob(os.path.join('/tmp/test_glob', glob_pattern)):
print('log_path: ', log_path)
but it returns all file from folder /tmp/test_glob/
I tried to modify this pattern like this:
glob_pattern = u'*.log.[0-9][0-9]'
but it returns only
/tmp/test_glob/client.log.11
/tmp/test_glob/client.log.32
How to fix this pattern ?
Using Pythex(a Python regex tester), the match string
glob_pattern = u'.*(\.log)(?!.*(gz)).*'
Worked well for your goal.
Try **/*.log!(*.gz)
Test using globster.xyz
That isn't a glob pattern. You don't want glob. You want to use the re module functions to filter the results of os.listdir.

Python and regex - how to find anytext_NUMBER_svm.pkl

I have file names that are in this format:
anytext_NUMBER_svm.pkl
I need to loop thourgh all files in a dir and file files that look like this:
file1.txt
file2.txt
anytext_1_svm.pkl
anytext_2_svm.pkl
anytext_3_svm.pkl
The matched files will be this:
anytext_1_svm.pkl
anytext_2_svm.pkl
anytext_3_svm.pkl
How to I use python regex to do this?
An option that:
doesn't use re
makes sure the comparison is only on the filename part - not part of a path
restricts the number of filename patterns to validate further using iglob
Code:
from glob import iglob
import os.path
for fname in iglob('*_*_svm.pkl'):
path, name = os.path.split(fname)
anytext, digit, rest = name.split('_', 2)
if digit.isdigit(): # add criteria for anytext if required...
# ....
This regex shoud solve your problems:
>>> import re
>>> regex = re.compile(r'.+_\d+_svm\.pkl')
>>> regex.search('anytext_1_svm.pkl') != None
True
But you should definitely take a look at the documentation: http://docs.python.org/library/re.html
I would suggest a review of this page:
http://docs.python.org/py3k/library/re.html#module-re
It will help you understand how to write regular expressions and ensure that you are matching things properly. For the number, use [0-9]*, use _ to separate your groups, and write a little match-checking conditional stuff and this will be a quick project.
import glob
file_list = glob.glob('anytext_[0-9]_svm.pk1')
regex to catch "anytext_NUMBER_svm.pkl" is very simple.
r'.+_\d+_svm\.pkl'

I don't understand the 'from' in Python [duplicate]

This question already has answers here:
Use 'import module' or 'from module import'?
(23 answers)
`from ... import` vs `import .` [duplicate]
(6 answers)
Closed 2 years ago.
from glob import glob
from os.path import isfile
def countwords(fp):
with open(fp) as fh:
return len(fh.read().split())
print "There are" ,sum(map(countwords, filter(isfile, glob("*.txt") ) ) ), "words in the files."
in the first line, why don't this just simply import glob library?
Is there any reason for using "from glob" in front of "import glob"?
If you write import glob, you would need to use glob.glob.
from glob import glob takes glob.glob and makes it available as just glob.
>>> import glob
>>> dir(glob)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'fnmatch', 'glob', 'glob0', 'glob1', 'has_magic', 'iglob', 'magic_check',
'os', 're', 'sys']
You can see all of the functions in the glob module here, you may for example want an iterator version of glob and in that case you would use:
from glob import iglob
if you use import glob, for the function glob("*.txt"), you have to write glob.glob("*.txt").
basically, the first glob in the from glob import glob is the name of the module, while the second one is the name of the function.
from glob import glob will import glob attribute of glob module when import glob will import whole module
This link seemed to explain all of the different variations quite well:
http://effbot.org/zone/import-confusion.htm
The bottom line is that importing a specific thing from a module gives you a direct reference to ONLY that thing and not the whole module.
When you have import glob, you import all the functions from that module. When you have from xyz import abc, you're only importing the abc function from the xyz module. In this case, you're importing the glob function from the glob module.
This way, in the Python code, if you want to use the glob function, instead of writing glob.glob, you only have to write glob.

Categories

Resources