本文目录导读:

透视分析的方法。
Python实现方案
基础文本分析脚本
#!/usr/bin/env python3
"""透视分析工具
"""
import os
import sys
import re
from collections import Counter
from pathlib import Path
class FileAnalyzer:
def __init__(self, filepath):
self.filepath = Path(filepath)
self.content = ""
self.stats = {}
def read_file(self):
"""读取文件内容"""
try:
with open(self.filepath, 'r', encoding='utf-8') as f:
self.content = f.read()
return True
except Exception as e:
print(f"读取文件失败: {e}")
return False
def analyze_structure(self):
"""分析文件结构"""
lines = self.content.split('\n')
self.stats['total_lines'] = len(lines)
self.stats['empty_lines'] = lines.count('')
self.stats['code_lines'] = self.stats['total_lines'] - self.stats['empty_lines']
# 分析缩进层次
indent_levels = []
for line in lines:
if line.strip():
indent = len(line) - len(line.lstrip())
indent_levels.append(indent)
if indent_levels:
self.stats['avg_indent'] = sum(indent_levels) // len(indent_levels)
self.stats['max_indent'] = max(indent_levels)
def analyze_content(self):
"""分析内容特征"""
# 单词频率
words = re.findall(r'\w+', self.content.lower())
self.stats['total_words'] = len(words)
self.stats['unique_words'] = len(set(words))
self.stats['word_frequency'] = Counter(words).most_common(10)
# 字符统计
self.stats['total_chars'] = len(self.content)
self.stats['char_no_space'] = len(self.content.replace(' ', '').replace('\n', ''))
# 特殊字符
special_chars = re.findall(r'[^\w\s]', self.content)
self.stats['special_chars_count'] = len(special_chars)
def detect_file_type(self):
"""检测文件类型"""
extension = self.filepath.suffix.lower()
type_map = {
'.py': 'Python',
'.js': 'JavaScript',
'.html': 'HTML',
'.css': 'CSS',
'.json': 'JSON',
'.xml': 'XML',
'.csv': 'CSV',
'.txt': '文本文件',
'.md': 'Markdown',
}
return type_map.get(extension, '未知类型')
def generate_report(self):
"""生成分析报告"""
report = f"""
╔══════════════════════════════════════╗
║ 文件内容透视分析报告 ║
╚══════════════════════════════════════╝
📁 文件信息
文件路径: {self.filepath}
文件大小: {os.path.getsize(self.filepath):,} bytes
文件类型: {self.detect_file_type()}
📊 结构分析
总行数: {self.stats.get('total_lines', 0):,}
代码行数: {self.stats.get('code_lines', 0):,}
空行数: {self.stats.get('empty_lines', 0):,}
平均缩进: {self.stats.get('avg_indent', 0)}
最大缩进: {self.stats.get('max_indent', 0)}
分析
总字符数: {self.stats.get('total_chars', 0):,}
非空格字符: {self.stats.get('char_no_space', 0):,}
总词汇数: {self.stats.get('total_words', 0):,}
唯一词汇: {self.stats.get('unique_words', 0):,}
特殊字符: {self.stats.get('special_chars_count', 0):,}
🏆 高频词汇 (Top 10):
"""
for word, count in self.stats.get('word_frequency', []):
report += f" {word}: {count}次\n"
return report
def main():
if len(sys.argv) != 2:
print("用法: python file_analyzer.py <文件路径>")
sys.exit(1)
analyzer = FileAnalyzer(sys.argv[1])
if analyzer.read_file():
analyzer.analyze_structure()
analyzer.analyze_content()
print(analyzer.generate_report())
else:
sys.exit(1)
if __name__ == "__main__":
main()
Bash脚本实现(Linux/Unix)
#!/bin/bash
# file_perspective.sh - 文件透视分析
FILE=$1
if [ ! -f "$FILE" ]; then
echo "文件不存在: $FILE"
exit 1
fi
echo "╔══════════════════════════════════════╗"
echo "║ 文件内容透视分析报表 ║"
echo "╚══════════════════════════════════════╝"
echo ""
# 基本文件信息
echo "📁 文件信息"
echo " 文件名: $(basename $FILE)"
echo " 大小: $(stat -f%z $FILE 2>/dev/null || stat -c%s $FILE) bytes"
echo " 权限: $(stat -f%Sp $FILE 2>/dev/null || stat -c%a $FILE)"
echo ""
# 行统计
TOTAL_LINES=$(wc -l < "$FILE")
EMPTY_LINES=$(grep -c '^$' "$FILE" 2>/dev/null || echo 0)
CODE_LINES=$((TOTAL_LINES - EMPTY_LINES))
echo "📊 行分析"
echo " 总行数: $TOTAL_LINES"
echo " 非空行: $CODE_LINES"
echo " 空行数: $EMPTY_LINES"
echo " 最大行长度: $(awk '{ if (length > max) max = length } END { print max }' $FILE)"
echo ""
# 字符分析
echo "📝 字符分析"
echo " 总字符数: $(wc -c < $FILE)"
echo " 单词数: $(wc -w < $FILE)"
echo " 唯一单词数: $(tr ' ' '\n' < $FILE | sort -u | wc -l)"
echo ""
模式分析
echo "🔍 特殊模式"
# 查找URL
echo " URL数量: $(grep -c 'https\?://' "$FILE" 2>/dev/null || echo 0)"
# 查找邮件
echo " 邮箱数量: $(grep -c '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]' "$FILE" 2>/dev/null || echo 0)"
# 查找数字
echo " 数字数量: $(grep -c '[0-9]' "$FILE" 2>/dev/null || echo 0)"
echo ""
# 高频词分析
echo "🏆 高频词 (Top 10):"
tr -c '[:alnum:]' '[\n*]' < "$FILE" | \
grep -v '^$' | \
sort | \
uniq -c | \
sort -rn | \
head -10 | \
awk '{printf " %s: %d次\n", $2, $1}'
高级分析:代码质量透视
#!/usr/bin/env python3
"""
代码质量透视分析器
"""
import ast
import re
from pathlib import Path
class CodeQualityAnalyzer:
def __init__(self, filepath):
self.filepath = Path(filepath)
self.issues = []
def analyze_python_code(self):
"""分析Python代码质量"""
with open(self.filepath, 'r', encoding='utf-8') as f:
code = f.read()
try:
tree = ast.parse(code)
# 分析函数复杂度
functions = [node for node in ast.walk(tree)
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))]
print(f"📊 函数分析")
print(f" 函数总数: {len(functions)}")
for func in functions:
complexity = self._calculate_complexity(func)
if complexity > 10:
print(f" ⚠️ 函数 {func.name}: 复杂度 {complexity} (建议重构)")
# 分析类和导入
classes = [node for node in ast.walk(tree)
if isinstance(node, ast.ClassDef)]
imports = [node for node in ast.walk(tree)
if isinstance(node, (ast.Import, ast.ImportFrom))]
print(f" 类定义: {len(classes)}")
print(f" 导入语句: {len(imports)}")
except SyntaxError as e:
print(f"❌ 语法错误: {e}")
def _calculate_complexity(self, func_node):
"""计算循环复杂度"""
complexity = 1
for node in ast.walk(func_node):
if isinstance(node, (ast.If, ast.While, ast.For,
ast.ExceptHandler, ast.With)):
complexity += 1
elif isinstance(node, ast.BoolOp):
complexity += len(node.values) - 1
return complexity
def analyze_comments_ratio(self):
"""分析注释比例"""
with open(self.filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
comment_lines = 0
docstring_lines = 0
code_lines = 0
for line in lines:
stripped = line.strip()
if stripped.startswith('#'):
comment_lines += 1
elif stripped.startswith('"""') or stripped.startswith("'''"):
docstring_lines += 1
elif stripped:
code_lines += 1
total = code_lines + comment_lines + docstring_lines
print(f"\n📈 注释分析")
print(f" 代码行: {code_lines} ({code_lines/total*100:.1f}%)")
print(f" 注释行: {comment_lines} ({comment_lines/total*100:.1f}%)")
print(f" 文档字符串: {docstring_lines} ({docstring_lines/total*100:.1f}%)")
if comment_lines / code_lines < 0.1:
print(" ⚠️ 建议: 注释比例偏低,建议增加注释")
多媒体文件透视
#!/usr/bin/env python3
"""
二进制文件透视分析
"""
import struct
import hashlib
class BinaryFileAnalyzer:
def __init__(self, filepath):
self.filepath = filepath
def analyze_header(self):
"""分析文件头信息"""
with open(self.filepath, 'rb') as f:
header = f.read(16)
# 文件签名识别
signatures = {
b'\x89PNG': 'PNG图片',
b'\xff\xd8\xff': 'JPEG图片',
b'GIF8': 'GIF图片',
b'%PDF': 'PDF文档',
b'PK\x03\x04': 'ZIP/Office文档',
b'\x7fELF': 'ELF可执行文件',
}
print("📋 文件签名分析:")
for sig, desc in signatures.items():
if header.startswith(sig):
print(f" ✅ 检测到: {desc}")
break
else:
print(f" ℹ️ 未知文件类型")
print(f" 文件头(HEX): {header.hex()}")
def calculate_entropy(self):
"""计算文件熵值"""
with open(self.filepath, 'rb') as f:
data = f.read()
# 计算字节频率
freq = {}
for byte in data:
freq[byte] = freq.get(byte, 0) + 1
# 计算熵值
import math
entropy = 0
for count in freq.values():
prob = count / len(data)
entropy -= prob * math.log2(prob)
print(f"\n📊 熵值分析:")
print(f" 文件熵值: {entropy:.2f} (理论最大值: 8.0)")
if entropy > 7:
print(" 状态: 高熵值 (可能已加密或压缩)")
elif entropy > 5:
print(" 状态: 中等熵值 (正常数据)")
else:
print(" 状态: 低熵值 (高度结构化数据)")
def get_file_hash(self):
"""计算文件哈希值"""
with open(self.filepath, 'rb') as f:
data = f.read()
print(f"\n🔐 文件哈希值:")
print(f" MD5: {hashlib.md5(data).hexdigest()}")
print(f" SHA1: {hashlib.sha1(data).hexdigest()}")
print(f" SHA256: {hashlib.sha256(data).hexdigest()}")
使用示例
# 使用Python脚本分析文本文件
python file_analyzer.py example.txt
# 使用Bash脚本
chmod +x file_perspective.sh
./file_perspective.sh example.txt
# 分析Python代码质量
python -c "
from file_analyzer import CodeQualityAnalyzer
analyzer = CodeQualityAnalyzer('example.py')
analyzer.analyze_python_code()
analyzer.analyze_comments_ratio()
"
这些脚本提供了文件内容的深度透视功能,可以根据具体需求选择合适的方案或组合使用。