当我们找到一处自己想要的代码资源时,却发现遇到了一个问题:
每行代码前都有行号(如图):
1. #include <iostream>
2. #include <time.h>
3. #include <vector>
4. #include <math.h>
5. #include <stdlib.h>
6. #include <algorithm>
7. #include <cstring>
8. #include <windows.h>
9. #include <fstream>
10. using namespace std;
11.
我们复制到txt中,得到一个很让人不开心的结果(如图):
这就让人很不开心,我们要手动删掉代码段前的行号,才能运行代码。如果有几百行,就非常累,非常不爽。
如果你是大佬,你当然不会担心这个问题。但根据我的经验,能看到这里,还能坚持往下读的同学,大部分都是小白级别的选手,为了阅读体验和实操方便,我会一步一步的进行教程和实现。
结合标题,你应该知道我们将使用python来实现该功能。这是由于python在文件处理方面的简单和上手难度低的原因。
我们使用pycharm来进行实现,我会一步步进行实现,你也可以跳过教程直达源代码进行copy:
首先,创建一个python工程,你会得到一个main.py文件:
接着,我们在和main.py相同目录的地方创建一个文件f.txt,然后把带行号的代码复制粘贴到里面去,就像图中这样:
当我们进行完这一步后,我们进行main.py中的处理函数的编写:
1.定义函数
func_file_process(file_path, new_file_path)
其中file_path是你的文件路径,new_file_path是你指定的新文件的路径。
2.将原来的文件中的内容读取到一个列表content中,这里使用python的api进行操作(我将编码方式设为utf-8,不然会报错,各位可以酌情修改):
with open(file_path, encoding='utf-8') as f:
content = f.read().splitlines()
for line in content:
print(line)
3.如果我们将content打印出来,会看到他是图中这样的:
注意:如果你的代码紧挨着行号,那也没有问题。
我们得到了content列表,接着要干的,是将它分离出来,那么进行分析,如果我们需要第一行,就要把 1. 删掉。 以此类推,把所有的行首的 "."前的东西全都删掉,就行了,问题来了,怎么删掉呢?
可以通过把字符串分割的方法,将列表进行批处理,比如对于字符串
"1. #include <iostream>",将其分为["1."," #include <iostream>"],然后我们只取后一个字符串,就ok了。
来看看怎么实现:
首先定义second_of_str(s1, s2)函数,用它来返回每个字符串中的第二段。
def second_of_str(s1, s2):
lt = s1.split(s2, 1) #这里的意思是找第一次s2第一次出现的位置
if len(lt) == 1:
return [] #如果找不到,就返回空列表,防止报错
return lt[1] #返回后一个字符串
接着我们遍历整个content,将其中的每一行都经second_of_str处理,然后放入new_content中。
new_content = []
for line in content:
new_line = second_of_str(line, '.')
new_content.append(new_line)
然后遍历new_content,进行打印,不出意外的话,你应该得到这样的结果:
for line in new_content:
print(line)
这时你已经可以直接复制粘贴了,但我喜欢将其打印在一个txt文件中:
new_f = open(new_file_path, encoding='utf-8', mode='w')
for line in new_content:
new_f.writelines(line)
new_f.writelines(["\n"])
new_f.close()
现在我们得到想要的资源了,快乐ing。
附上全部源代码:
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
def second_of_str(s1, s2):
lt = s1.split(s2, 1)
if len(lt) == 1:
return []
return lt[1]
def func_file_process(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, '.')
new_content.append(new_line)
for line in new_content:
print(line)
new_f = open(new_file_path, encoding='utf-8', mode='w')
for line in new_content:
new_f.writelines(line)
new_f.writelines(["\n"])
new_f.close()
return new_content
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
f = func_file_process('f.txt', 'new_f.txt')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
tips: 作为python半吊子选手,一年写不了几次python,创作不易,还望客官赏个赞。
如果对你有帮助,欢迎点赞评论呀。