i have a list of strings i am looping through. in the loop i want to only do something…
- User
i have a list of strings i am looping through. in the loop i want to only do something if the string does not contain :
- Assistant
You can check if a string does not contain a specific character, such as ":", by using the
not inoperator. Here's an example of how you can achieve this within a loop:my_list = ["string1", "string2:", "string3", "string4:"] for string in my_list: if ":" not in string: # do something print(f"{string} does not contain ':'")In this example, the loop iterates over each string in the
my_listlist. Theifstatement checks if the string does not contain a ":" character using thenot inoperator. If the condition is true, it means that the string does not contain ":", and you can perform the desired actions inside theifblock. - User
that is still returning things like 'Module:Authority control/config'
- Assistant
If you want to exclude the strings that contain ":" anywhere in the string, you can use the
str.find()method orstr.index()method. Here's an updated example:my_list = ["string1", "string2:", "string3", "string4:", "Module:Authority control/config"] for string in my_list: if string.find(":") == -1: # do something print(f"{string} does not contain ':'")The
find()method returns the index of the first occurrence of the specified substring (in this case, ":"). If the substring is not found, it returns -1. Hence, by checking ifstring.find(":")is equal to -1, we can determine if ":" is not present in the string. If the condition is true, the code will execute the desired action for strings that do not contain ":". - User
for i in tqdm(webpage_list): # Initialize a dictionary to store title and content page_data = {}
# Save title page_data['title'] = i # Fetch the Wikipedia page par_page = wiki_wiki.page(i) # Extract page text page_data['content'] = par_page.text # Find links and scrape them for title in par_page.links.keys(): if ":" not in title: # Initialize a dictionary to store title and content of nested nested_page_data = {} # Save title nested_page_data['title'] = i # Fetch the Wikipedia page nested_page = wiki_wiki.page(title) # Extract page text nested_page_data['content'] = nested_page.texthow do i append each nested_pade_data to page_data dict.
- Assistant
To append each
nested_page_datadictionary to thepage_datadictionary, you can create a new key inpage_datacalled "nested_data" and assign it a list. Then, you can append eachnested_page_datadictionary to this list. Here's an updated version of your code:from tqdm import tqdm page_data = [] for i in tqdm(webpage_list): # Initialize a dictionary to store title and content data = {} # Save title data['title'] = i # Fetch the Wikipedia page par_page = wiki_wiki.page(i) # Extract page text data['content'] = par_page.text # Initialize a list to store nested page data nested_data_list = [] # Find links and scrape them for title in par_page.links.keys(): if ":" not in title: # Initialize a dictionary to store title and content of nested page nested_page_data = {} # Save title nested_page_data['title'] = title # Fetch the Wikipedia page nested_page = wiki_wiki.page(title) # Extract page text nested_page_data['content'] = nested_page.text # Append the nested page data to the nested data list nested_data_list.append(nested_page_data) # Add the nested data list to the main data dictionary data['nested_data'] = nested_data_list # Append the main data dictionary to the page_data list page_data.append(data)