AttributeError: 'NoneType' object has no attribute 'group' in python
I'm following Automatic boring stuf with python enter image description here
do you know?
how many words do you know
See also questions close to this topic
-
Python File Tagging System does not retrieve nested dictionaries in dictionary
I am building a file tagging system using Python. The idea is simple. Given a directory of files (and files within subdirectories), I want to filter them out using a filter input and tag those files with a word or a phrase.
If I got the following contents in my current directory:
data/ budget.xls world_building_budget.txt a.txt b.exe hello_world.dat world_builder.spec
and I execute the following command in the shell:
py -3 tag_tool.py -filter=world -tag="World-Building Tool"
My output will be:
These files were tagged with "World-Building Tool": data/ world_building_budget.txt hello_world.dat world_builder.spec
My current output isn't exactly like this but basically, I am converting all files and files within subdirectories into a single dictionary like this:
def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree
Right now, my dictionary looks like this:
key:''
.In the following function, I am turning the empty values
''
into empty lists (to hold my tags):def empty_str_to_list(d): for k,v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v)
When I run my entire code, this is my output:
hello_world.dat ['World-Building Tool'] world_builder.spec ['World-Building Tool']
But it does not see
data/world_building_budget.txt
. This is the full dictionary:{'data': {'world_building_budget.txt': []}, 'a.txt': [], 'hello_world.dat': [], 'b.exe': [], 'world_builder.spec': []}
This is my full code:
import os, argparse def fs_tree_to_dict(path_): file_token = '' for root, dirs, files in os.walk(path_): tree = {d: fs_tree_to_dict(os.path.join(root, d)) for d in dirs} tree.update({f: file_token for f in files}) return tree def empty_str_to_list(d): for k, v in d.items(): if v == '': d[k] = [] elif isinstance(v, dict): empty_str_to_list(v) parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--filter", action="store", help="keyword to filter files") parser.add_argument("--tag", action="store", help="a tag phrase to attach to a file") parser.add_argument("--get_tagged", action="store", help="retrieve files matching an existing tag") args = parser.parse_args() filter = args.filter tag = args.tag get_tagged = args.get_tagged current_dir = os.getcwd() files_dict = fs_tree_to_dict(current_dir) empty_str_to_list(files_dict) for k, v in files_dict.items(): if filter in k: if v == []: v.append(tag) print(k, v) elif isinstance(v, dict): empty_str_to_list(v) if get_tagged in v: print(k, v)
-
Actaully i am working on a project and in it, it is showing no module name pip_internal plz help me for the same. I am using pycharm(conda interpreter
File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\Scripts\pip.exe\__main__.py", line 4, in <module> File "C:\Users\pjain\AppData\Local\Programs\Python\Python310\lib\site-packages\pip\_internal\__init__.py", line 4, in <module> from pip_internal.utils import _log
I am using pycharm with conda interpreter.
-
Looping the function if the input is not string
I'm new to python (first of all) I have a homework to do a function about checking if an item exists in a dictionary or not.
inventory = {"apple" : 50, "orange" : 50, "pineapple" : 70, "strawberry" : 30} def check_item(): x = input("Enter the fruit's name: ") if not x.isalpha(): print("Error! You need to type the name of the fruit") elif x in inventory: print("Fruit found:", x) print("Inventory available:", inventory[x],"KG") else: print("Fruit not found") check_item()
I want the function to loop again only if the input written is not string. I've tried to type return Under print("Error! You need to type the name of the fruit") but didn't work. Help
-
How to get the domain and string to the first slash
For example like this
url = https://example.com/test/myname/myname.jpg result = re.search('(?:https?://)?(?P<host>.*?)(?:[:#?/@]|$)', url)
I can get the
example.com
with this.However I want to get the
example.com/test
(string before first/
)how can I do this?
-
How to check if data URI is valid in PHP?
How to validate data uri ex: "data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2..."
-
Extracting Date from String with Regular Expressions for Python
Can anyone tell me why the following isn't working:
I am reading lines from a file, some lines have a date at the beginning and some lines do not. I wanted to use regex to detect if a line I am reading in from the file has a date at the beginning, if it does then I want to do some further parsing. If it does not, I just want to ignore it. The following is a sample line:
" JAN 01 19 BALANCE FORWARD: 32 CHQ PLAN 33 222.13 "
When I use the re.findall, it appears my regular expression is incorrect as I am hoping to extract the date "JAN 01 19" but instead all I get returned is "JAN":
line = " JAN 01 19 BALANCE FORWARD: 32 CHQ PLAN 33 222.13 " print(re.findall(r"(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) \d{2} \d{2}", line))
Output:
['JAN']
What I am hoping to see is:
['JAN 01 19']
From what I understand of regex, the
(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)
part should be checking for one of these months in the string, I then have a space and then I am looking for a 2 digit number with\d{2}
, then another space and then another 2 digit number with\d{2}
.I tried testing this out on https://regex101.com/ and I seem to get a match of 'JAN 01 19' so I am really unsure why the regular expression is not working when I move this to Python.
-
"no main manifest attribute" in .jar Netbeans 13
I recently just started toying around with Maven in java. Time comes to test my project, it works fine in the NetBeans window, running the main class found in App.java (com.MyCompany.App), but when I try to run it from a command line I get an error:
"no main manifest attribute"
I'm using Apache Netbeans 13 with Maven to build the project
Any help will be apreciated Thanks in advance
-
Retrieve C# Attribute Data With Extension Method
Looking for a way to retrieve attribute data for a property on a class (many different types) in my data layer with an extension method...
Final goal is to see the method here and be able to access the properties:
I would expect to be able to do something like (non-working code):
Customer c = new Customer(); int mcl = c.CustNo.GetSchemaDetails().MaxCharLength;
The attribute is assigned to the class property as such:
public class Customer { [ColumnSchema("Customer", "CUST_NO")] public string CustNo { get; set; } //... }
I've created an extension method to retrieve the 'SchemaDetails' object from the 'ColumnSchemaAttribute'
public class AttributeExtensions { public static SchemaDetails GetSchemaDetails(Type T) { ColumnSchemaAttribute csa = (ColumnSchemaAttribute)Attribute.GetCustomAttribute(T, typeof(ColumnSchemaAttribute)); return csa.SchemaDetails; } }
Bulk of the 'ColumnSchemaAttribute' which returns db schema details for the column
public class ColumnSchemaAttribute : System.Attribute { private string tableName, columnName; public ColumnSchemaAttribute(string TableName, string ColumnName) { tableName = TableName; columnName = ColumnName; } private SchemaDetails schemaDetails; public SchemaDetails SchemaDetails { get { if (schemaDetails == null) { schemaDetails = getSchemaDetails(); } return schemaDetails; } } private SchemaDetails getSchemaDetails() { SchemaDetails sd = null; string sql = @" SELECT * FROM INFORMATION_SCHEMA.COLUMNS c LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu ON ccu.COLUMN_NAME = c.COLUMN_NAME and ccu.TABLE_NAME = c.TABLE_NAME LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON tc.CONSTRAINT_NAME = ccu.CONSTRAINT_NAME and tc.TABLE_NAME = ccu.TABLE_NAME WHERE c.TABLE_NAME = @TableName and c.COLUMN_NAME = @ColumnName "; try { using (SqlConnection sqlConnection = new SqlConnection(Helper.GetConnectionString())) { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection)) { sqlCommand.CommandType = CommandType.Text; sqlCommand.Parameters.Add("@TableName", SqlDbType.NVarChar, -1).Value = tableName; sqlCommand.Parameters.Add("@ColumnName", SqlDbType.NVarChar, -1).Value = columnName; SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); while (sqlDataReader.Read()) { sd = readData(sqlDataReader); } } } } catch (Exception ex) { throw new System.ArgumentException(ex.Message); } return sd; } }
Any help regarding a way to do this or an alternative approach is very much appreciated.
-
woocommerce product variation color attributes on product list page redirect to single product page on-click
I want, when I click on these color attributes on the product list page it redirects me automatically to the single product page. it shows similar behavior to the title of the product. but I want this functionality only for the product list page, not for a single product page. please help thank you. enter image description here
-
Tableau Top N set to 10 of group set is showing more than 10
How can I get top 10 results of my longest rides to date or filter the results to grab distinct string which would be the [Title] of the bike ride? Is there a custom formula I can enter?
My result is showing two results for Ride 1 and Ride 6 which are identical. Therefore I'm getting 12 results instead of 10. As the year goes on, I may repeat a ride and continue to run into this so I'm looking to find a solution that would cover any duplicate titled ride.
on my group set, by field, Top 10 Distance, AVG or MAX (shows the dupes for two rides)
On my Top N parameter I've tried: By field, Top 10 Distance Sum (this shows all rides which is more than 10)
I've done: By field, Top 10 Distance Max (this shows the dupes for two rides)
I've done: By field, Top 10 Distance Avg (this also shows the dupes for two rides)
-
Is it possible to create a dynamic device group membership based on the existence of a file on the local drive within Azure?
Looking for a way to create a dynamic device group membership based on a single file on the local drive in Azure. Tried utilizing Powershell within the rule syntax section, but it appears to only accept code based on the rule builder. Thanx in advance.