Nghe Si Chu Van Quyenh
Junior Member
Chào các bác,
E có đoạn code sau mô phỏng lệnh wc trong linux, mặc định là in ra số dòng, từ, ký tự từ file, nếu nhiều file thì in tương tự từng file một, và chốt là tổng dòng, từ, ký tự.
Tùy theo option truyền vào mà chỉ in ra tương ứng giá trị , ví dụ nếu đưa -w vào thì chỉ in số ký tự, -wc thì chỉ in số từ và ký tự.
Code hiện giờ nhiều if quá, nhìn rối rắm nhưng e ko biết làm sao để rút bớt xuống. Nhờ các bác gợi ý ạ.
E có đoạn code sau mô phỏng lệnh wc trong linux, mặc định là in ra số dòng, từ, ký tự từ file, nếu nhiều file thì in tương tự từng file một, và chốt là tổng dòng, từ, ký tự.
Tùy theo option truyền vào mà chỉ in ra tương ứng giá trị , ví dụ nếu đưa -w vào thì chỉ in số ký tự, -wc thì chỉ in số từ và ký tự.
Code hiện giờ nhiều if quá, nhìn rối rắm nhưng e ko biết làm sao để rút bớt xuống. Nhờ các bác gợi ý ạ.
Python:
#!/usr/bin/env python
import sys
import argparse
# --------------------------------------------------
def get_args():
parser = argparse.ArgumentParser(
description='Emulate wordcount',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('file', nargs="*",
metavar='FILE',type=argparse.FileType('rt'),default=[sys.stdin],
help='Input files(s)')
parser.add_argument('-c', '--character', help='number of characters', action='store_true')
parser.add_argument('-l', '--line',help='number of lines',action='store_true')
parser.add_argument('-w', '--word', help='number of words',action='store_true')
return parser.parse_args()
# --------------------------------------------------
def main():
args = get_args()
file_arg = args.file
#get each argument
character_args = args.character
line_args = args.line
word_args = args.word
total_line, total_words, total_byte = 0, 0, 0
for fh in file_arg:
line_count, words_count, byte_count = 0, 0, 0
for line in fh:
line_count += 1
byte_count += len(line)
words_count += len(line.split())
total_line += 1
total_words += len(line.split())
total_byte += len(line)
if character_args and not line_args and not word_args:
print(f'{byte_count:8} {fh.name}')
if line_args and not character_args and not word_args:
print(f'{line_count:8} {fh.name}')
if word_args and not character_args and not line_args:
print(f'{words_count:8} {fh.name}')
if character_args and line_args and not word_args:
print(f'{byte_count:8}{line_count:8} {fh.name}')
if character_args and word_args and not line_args:
print(f'{byte_count:8}{words_count:8} {fh.name}')
if line_args and word_args and not character_args:
print(f'{line_count:8}{words_count:8} {fh.name}')
if line_args and word_args and character_args:
print(f'{line_count:8}{words_count:8}{byte_count:8} {fh.name}')
if not line_args and not word_args and not character_args:
print(f'{line_count:8}{words_count:8}{byte_count:8} {fh.name}')
if len(file_arg) > 1:
if character_args and not line_args and not word_args:
print(f'{total_byte:8} total')
if line_args and not character_args and not word_args:
print(f'{total_line:8} total')
if word_args and not character_args and not line_args:
print(f'{total_words:8} total')
if character_args and line_args and not word_args:
print(f'{total_byte:8}{total_line:8} total')
if character_args and word_args and not line_args:
print(f'{total_byte:8}{total_words:8} total')
if line_args and word_args and not character_args:
print(f'{total_line:8}{total_words:8} total')
if line_args and word_args and character_args:
print(f'{total_line:8}{total_words:8}{total_byte:8} total')
if not line_args and not word_args and not character_args:
print(f'{total_line:8}{total_words:8}{total_byte:8} total')
# --------------------------------------------------
if name == 'main':
main()
