当前位置:网站首页>Lesson 029: Documents: a task? After class test questions and answers

Lesson 029: Documents: a task? After class test questions and answers

2022-06-22 21:35:00 ChaseTimLee

use one's hands :

0. Write a program , Accept user input and save it as a new file , The program implementation is shown in the figure :

Alt

def file_write(file_name):
   f = open(file_name, 'w')
   print(' Please enter the content 【 Separate input \':w\' Save and exit 】:')

   while True:
       write_some = input()
       if write_some != ':w':
           f.write('%s\n' % write_some)
       else:
           break

   f.close()

file_name = input(' Please enter filename :')
file_write(file_name)

1. Write a program , Compare two files entered by the user , If different , Show all the different line numbers and the position of the first different character , The program implementation is shown in the figure :

Alt

def file_compare(file1, file2):
    f1 = open(file1)
    f2 = open(file2)
    count = 0 #  The statistical number of rows 
    differ = [] #  Count different numbers 

    for line1 in f1:
        line2 = f2.readline()
        count += 1
        if line1 != line2:
            differ.append(count)

    f1.close()
    f2.close()
    return differ

file1 = input(' Please enter the first file name to compare :')
file2 = input(' Please enter another file name to compare :')

differ = file_compare(file1, file2)

if len(differ) == 0:
    print(' The two files are exactly the same !')
else:
    print(' The two files share 【%d】 Different :' % len(differ))
    for each in differ:
        print(' The first  %d  It's different ' % each)

2. Write a program , When the user enters the file name and the number of lines (N) after , Put the front of the file N The line content is printed to the screen , The program implementation is shown in the figure :

Alt

def file_view(file_name, line_num):
    print('\n file %s Before %s Is as follows :\n' % (file_name, line_num))
    f = open(file_name)
    for i in range(int(line_num)):
        print(f.readline(), end= '')

    f.close()

file_name = input(r' Please enter the file to open (C:\\test.txt):')
line_num = input(' Please enter the first few lines of the file to be displayed :')
file_view(file_name, line_num)

3. Uh , It has to be said that our users are becoming more and more tricky . It is required to expand on the previous question , The user can input the number of lines to be displayed at will .( Such as the input 13:21 Print page 13 Go to the first place 21 That's ok , Input :21 Before printing 21 That's ok , Input 21: Then print from 21 Everything from the beginning of the line to the end of the file )

Alt

def file_view(file_name, line_num):
    if line_num.strip() == ':':
        begin = '1'
        end = '-1'
        
    (begin, end) = line_num.split(':')

    if begin == '':
        begin = '1'
    if end == '':
        end = '-1'

    if begin == '1' and end == '-1':
        prompt = ' Full text '
    elif begin == '1':
        prompt = ' From the beginning to %s' % end
    elif end == '-1':
        prompt = ' from %s To end ' % begin
    else:
        prompt = ' From %s Go to the first place %s That's ok ' % (begin, end)

    print('\n file %s%s Is as follows :\n' % (file_name, prompt))

    begin = int(begin) - 1
    end = int(end)
    lines = end - begin

    f = open(file_name)  
    
    for i in range(begin):  #  To consume begin Previous content 
        f.readline()

    if lines < 0:
        print(f.read())
    else:
        for j in range(lines):
            print(f.readline(), end='')
    
    f.close()

file_name = input(r' Please enter the file to open (C:\\test.txt):')
line_num = input(' Please enter the number of rows to display 【 The format is as follows  13:21  or  :21  or  21:  or  : 】:')
file_view(file_name, line_num)

4. Write a program , Realization “ All replacement ” function , The program implementation is shown in the figure :

Alt


def file_replace(file_name, rep_word, new_word):
    f_read = open(file_name)

    content = []
    count = 0

    for eachline in f_read:
        if rep_word in eachline:
            count = eachline.count(rep_word) #count I think I should use this 
            eachline = eachline.replace(rep_word, new_word)
        content.append(eachline)    

    decide = input('\n file  %s  The Communist Party of China has %s individual 【%s】\n Are you sure you want to put all the 【%s】 Replace with 【%s】 Do you ?\n【YES/NO】:' \
                   % (file_name, count, rep_word, rep_word, new_word))

    if decide in ['YES', 'Yes', 'yes']:
        f_write = open(file_name, 'w')
        f_write.writelines(content)
        f_write.close()

    f_read.close()


file_name = input(' Please enter filename :')
rep_word = input(' Please enter the word or character to be replaced :')
new_word = input(' Please enter a new word or character :')
file_replace(file_name, rep_word, new_word)

原网站

版权声明
本文为[ChaseTimLee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222005194016.html