can u help me with my code Search for a query string (e.g. “entertaining”, “disgusting”…
- User
can u help me with my code
Search for a query string (e.g. “entertaining”, “disgusting” or “main male character”) within the files zipped in ʻmaterials/aclimdb_dataset.zip” ○ The program should output the name of the file and how often the search term shows up in the file. ○ Order the files by the count of the searched term, starting from the file with the highest number of hits.
import zipfile
import os
Task 01
def find_files(filename, search_path):
result = [] for root, dirs, files in os.walk(search_path): if filename in files: result.append(os.path.join(root, filename)) return result
print(find_files("filename", "search_path"))
[]
def search_str(file_path, word):
with open(file_path, 'rb') as file: content = file.read().decode('utf-8', 'ignore') if word in content: print('string exist in a file') else: print('string does not exist in a file')search_str('C:\Users\selin\Documents\Volltext\aclimdb_dataset.zip', 'entertaining')
string does not exist in a file
- Assistant
- User
Cell In[4], line 4 with zipfile.ZipFile(zip_path, ‘r’) as z: ^ SyntaxError: invalid character '‘' (U+2018)
- Assistant
The syntax error you encountered is due to the use of the wrong type of quotation marks around the character 'r' in the code. In Python, and most programming languages, you should use straight single (' ') or double (" ") quotation marks for string literals, not curly quotes (‘ ’ or “ ”), which are often introduced by text editors like Microsoft Word or by copying code from formatted text sources.
Here's that specific line corrected:
with zipfile.ZipFile(zip_path, 'r') as z:So, the revised and correct portion of the code should be:
import zipfile import os def search_in_zip(zip_path, query): with zipfile.ZipFile(zip_path, 'r') as z: counts_dict = {} for file_info in z.infolist(): if not file_info.is_dir(): with z.open(file_info.filename) as file: content = file.read().decode('utf-8', 'ignore') count = content.lower().count(query.lower()) if count > 0: counts_dict[file_info.filename] = count for file_name, count in sorted(counts_dict.items(), key=lambda item: item[1], reverse=True): print(f"{file_name}: {count}") # Ensure to adjust the path to the actual location of your zip file search_in_zip('aclimdb_dataset.zip', 'entertaining') - User
Write a program that searches for a famous phrase within the files “goethe-faust_i….txt” zipped in “materials/20240321.zip” ○ Die Botschaft hör’ ich wohl, allein mir fehlt der Glaube ○ You should be able to find the this phrase in every file that is named “goethe-faust_i_…” ○ Please output the line number within the text.
- Assistant