import matplotlib.pyplot as plt
import hashlib
def file_processing_function(file_path):
unique_lines = set()
with open (file_path, 'r') as f:
for line in f:
unique_lines.add(line.rstrip())
return unique_lines
def sha256_calc(line):
m = hashlib.sha256(line.encode('utf-8'))
return m.hexdigest()
def sha256_to_int(hex_digest):
return int(hex_digest, 16)
def file_processing(file_path):
unique_lines = file_processing_function(file_path)
results = []
for line in unique_lines:
hex_digest = sha256_calc(line)
integer_value = sha256_to_int(hex_digest)
results.append((line, hex_digest, integer_value))
return results
def printer(results):
for line, hex_digest, integer_value in results:
print(f'Line: {line} \n SHA-256: {hex_digest} \n Int:{integer_value}\n')
def plot_histogram(results, bins = 20):
integer_values = [int(integer_value) for _, _, integer_value in results if isinstance(integer_value, int)]
plt.figure(figsize=(10, 6))
plt.hist(integer_values, bins = bins, color = 'purple', edgecolor = 'black')
plt.title(f'Histogram Based on SHA-256 of {file_path}')
plt.xlabel('Integer Values')
plt.ylabel('Frequency')
plt.grid(axis='y', linestyle='--', linewidth=0.7, alpha=0.7)
plt.show()
file_path= 'wordcount.txt'
results = file_processing(file_path)
printer(results)
plot_histogram(results, bins = 20)
这是我到目前为止所拥有的,但是该代码正在带回typeerror
Traceback (most recent call last):
File "c:\Users\serle\OneDrive\Desktop\Pyprojects\Hash Reader\Hash Reader Sha-256.py", line 53, in <module>
plot_histogram(results, bins = 20)
File "c:\Users\serle\OneDrive\Desktop\Pyprojects\Hash Reader\Hash Reader Sha-256.py", line 43, in plot_histogram
plt.hist(integer_values, bins = bins, color = 'purple', edgecolor = 'black')
File "C:\Users\serle\AppData\Local\Programs\Python\Python312\Lib\site-packages\matplotlib\pyplot.py", line 3440, in hist
return gca().hist(
^^^^^^^^^^^
File "C:\Users\serle\AppData\Local\Programs\Python\Python312\Lib\site-packages\matplotlib\__init__.py", line 1473, in inner
return func(
^^^^^
File "C:\Users\serle\AppData\Local\Programs\Python\Python312\Lib\site-packages\matplotlib\axes\_axes.py", line 7001, in hist
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\serle\AppData\Local\Programs\Python\Python312\Lib\site-packages\numpy\lib\_histograms_impl.py", line 797, in histogram
bin_edges, uniform_bins = _get_bin_edges(a, bins, range, weights)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\serle\AppData\Local\Programs\Python\Python312\Lib\site-packages\numpy\lib\_histograms_impl.py", line 430, in _get_bin_edges
first_edge, last_edge = _get_outer_edges(a, range)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\serle\AppData\Local\Programs\Python\Python312\Lib\site-packages\numpy\lib\_histograms_impl.py", line 314, in _get_outer_edges
if not (np.isfinite(first_edge) and np.isfinite(last_edge)):
^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
我对matplotlib不太好,有什么想法吗?
def plot_histogram(results, file_path, bins=20):
integer_values = []
for _, _, integer_value in results:
try:
integer_value = int(integer_value)
if np.isfinite(integer_value):
integer_values.append(integer_value)
except (ValueError, TypeError):
continue # Skip if the value is not a valid integer
plt.figure(figsize=(10, 6))
plt.hist(integer_values, bins=bins, color='purple', edgecolor='black')
plt.title(f'Histogram Based on SHA-256 of {file_path}')
plt.xlabel('Integer Values')
plt.ylabel('Frequency')
plt.grid(axis='y', linestyle='--', linewidth=0.7, alpha=0.7)
plt.show()
这很可能会起作用:
通向
也在
plot_histogram()
转换周围添加了一个
try-except
integer_value
并检查
int