Python deletes the line number of each line of the file

Time:2022-5-23
catalogue
  • 1. what
  • 2. Ideas
  • 3. Code

1. what

Python deletes the line number of each line of the file

This line number is really annoying. Try to write a py

2. Ideas

  • def second_of_strSplit, take the element to the right of the separator and return a list
  • def movePass in the file path, read each line, store the list, and call second_ of_ The str split is stored in a new list
  • Main function call move

3. Code

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def second_of_str(str,splitsymbol):
    s = str.split(splitsymbol,1)   #Element to the right of the separator
    # if  len(s) == 1:
    #     return []
    return s[1]
 
def move(file_path, new_file_path):
    with open(file_path,encoding='utf-8') as f:
        content = f.read().splitlines()
 
    # for line in content:
    #     print(line)
 
    new_content = []
    for line in content:
        new_line = second_of_str(line,'.')
        #Add a new list for each row after removal
        new_content.append(new_line)
 
    for line in new_content:
        print(line)
 
    f1 = open(new_file_path,encoding='utf-8',mode = 'w')
    for line in new_content:
        f1.writelines(line)
        f1.writelines(["\n"])
    f1.close()
    return new_content
 
if __name__ == '__main__':
    f = move('1.txt','2.txt')

Done, results~

Python deletes the line number of each line of the file

This is the end of this article on the interpretation of the line number idea of Python deleting each line of the file. For more information about Python deleting the line number of the file, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!