Paylaşım için teşekkürler, kodunda ufak tefek iyilleştirmeler yaptım. Regex ile precompiled bir pattern ile aramak çok daha performanslı olacaktır, ayrıca error handling kısmında hata alanlar için ekstra detaylar var.
Python:
import re
import os
try:
from pathlib import Path
except ImportError:
os.system("pip install pathlib")
from pathlib import Path
def load_item_names(item_names_filename: str) -> dict[str, str]:
item_names = {}
try:
with open(item_names_filename, "r", encoding="windows-1254", errors="replace") as f:
for line in f:
line = line.strip()
if not line or line.startswith("VNUM"):
continue
parts = line.split("\t", 1) if "\t" in line else line.split(None, 1)
if len(parts) < 2:
continue
item_names[parts[0]] = parts[1].strip()
except UnicodeDecodeError as e:
print(f"Encoding error in {item_names_filename}: {e}")
print("Using replacement characters for invalid bytes")
except FileNotFoundError:
print(f"Error: {item_names_filename} not found")
except Exception as e:
print(f"Error reading {item_names_filename}: {e}")
return item_names
def annotate_mob_drop_items(mob_drop_filename: str, item_names: dict, output_filename: str) -> None:
pattern = re.compile(r"^\d+\s+(\d+)")
try:
with open(mob_drop_filename, "r", encoding="windows-1254", errors="replace") as fin, \
open(output_filename, "w", encoding="windows-1254") as fout:
for line in fin:
if match := pattern.match(line.strip()):
vnum = match.group(1)
if vnum in item_names:
fout.write(f"{line.rstrip()} -- {item_names[vnum]}\n")
continue
fout.write(line)
except UnicodeDecodeError as e:
print(f"Encoding error in {mob_drop_filename}: {e}")
print("Using replacement characters for invalid bytes")
except FileNotFoundError:
print(f"Error: {mob_drop_filename} not found")
except Exception as e:
print(f"Error processing files: {e}")
def main() -> None:
current_dir = Path.cwd()
item_names_file = current_dir / "item_names.txt"
mob_drop_file = current_dir / "mob_drop_item.txt"
output_file = current_dir / "new_mob_drop_item.txt"
if not item_names_file.exists() or not mob_drop_file.exists():
print("Item names or mob drop file not found")
return
item_names = load_item_names(item_names_file)
if item_names:
annotate_mob_drop_items(mob_drop_file, item_names, output_file)
print(f"Completed: {output_file}")
if __name__ == "__main__":
main()
Şu an konuyu görüntüleyenler (Toplam : 0, Üye: 0, Misafir: 0)
Benzer konular
- Cevaplar
- 13
- Görüntüleme
- 2K
- 1.00 yıldız(lar) 1 Değerlendirme
- Cevaplar
- 2
- Görüntüleme
- 626
- Cevaplar
- 215
- Görüntüleme
- 50K
