PyMyFile是一个python库,用于以最简单的方式处理高级文件
pymyfile的Python项目详细描述
PyMyFile文件
PyMyFile是一个python库,用于以最简单的方式进行高级文件处理
安装
pip install pymyfile
使用
考虑一个文件“示例.txt“包含以下内容
^{pr2}$首先导入库并启动file对象
importpymyfilemyfile=pymyfile.file("sample.txt")
然后调用函数将对文件进行所需的更改,下面是对每个函数的berief解释。
读()
这将返回文件的内容,不需要关闭()。在
读取行(line_num)
这将返回指定的
>>>content=myfile.readline(line_num=3)>>>print(content)Thisisthirdlineofthefile.
readlines(from_line_num,to_line_num)
这将返回范围从_line_num到_to_line_num的行。这将包括两端。在
>>>contents=myfile.readlines(from_line_num=2,to_line_num=5))>>>print(content)Thisissecondlineofthefile.Thisisthirdlineofthefile.Thisisfourthlineofthefile.Thisisfifthlineofthefile.
追加(数据)
这将向文件追加数据,不需要关闭()。在
>>>myfile.append(data="This is appended line.")
这将是示例.txt. 在
This is first line of the file.
This is second line of the file.
This is third line of the file.
This is fourth line of the file.
This is fifth line of the file.
This is sixth line of the file.
.
.
.
.
This is n(th) line of the file.
This is appended line.
追加行(行数,数据)
这将向该行追加数据
>>>myfile.append_line(line_num=5,data=" This is appended data to line.")
这将是示例.txt. 在
This is first line of the file.
This is second line of the file.
This is third line of the file.
This is fourth line of the file.
This is fifth line of the file. This is appended data to line.
This is sixth line of the file.
.
.
.
.
This is n(th) line of the file.
写入(数据)
这将写入文件
>>>myfile.write("This is some data.")
这将是示例.txt. 在
This is some data.
写入行(行数,数据)
这将写入文件的特定行。在
>>>myfile.write_line(line_num=2,data="This is new content of the line.")
这将是示例.txt. 在
This is first line of the file.
This is new content of the line.
This is third line of the file.
This is fourth line of the file.
This is fifth line of the file.
This is sixth line of the file.
.
.
.
.
This is n(th) line of the file.
替换_-word(line_num,old_-word,new-word,occurrence=ALL,from_word_num=None,到_-word_num=None)
这将替换该行中的特定单词
>>>myfile.replace_word(line_num=1,old_word="file",new_word="some word")
这将是示例.txt. 在
1. This is first line of the some word.
2. This is second line of the file.
3. This is third line of the file.
4. This is fourth line of the file.
5. This is fifth line of the file.
6. This is sixth line of the file.
.
.
.
.
n. This is n(th) line of the file.
numiphy()
这将在每行的开头添加数字
>>>myfile.numiphy()
这将是示例.txt. 在
1. This is first line of the file.
2. This is second line of the file.
3. This is third line of the file.
4. This is fourth line of the file.
5. This is fifth line of the file.
6. This is sixth line of the file.
.
.
.
.
n. This is n(th) line of the file.
去湿()
这将还原numiphy()引起的更改
>>>myfile.numiphy()
这将是最终的内容示例.txt. 在
^{pr2}$bullettiphy()
这将在每行的开头添加项目符号“•”。在
>>>myfile.bulletiphy()
这将是示例.txt. 在
^{pr21}$去毛刺()
这将还原bullettiphy()引起的更改。在
>>>myfile.debulletiphy()
这将是示例.txt. 在
^{pr2}$asteriphy()
这与bullettiphy()相同,但它将添加“*”而不是bullet。在
>>>myfile.asteriphy()
这将是示例.txt. 在
* This is first line of the file.
* This is second line of the file.
* This is third line of the file.
* This is fourth line of the file.
* This is fifth line of the file.
* This is sixth line of the file.
.
.
.
.
* This is n(th) line of the file.
去绝育()
这将还原asteriphy()引起的更改。在
>>>myfile.deasteriphy()
这将是示例.txt. 在
^{pr2}$物理层(phy)
这与asteriphy()或bullettiphy()相同,但添加了自定义phy。在
>>>myfile.phy(phy="hmm")
这将是示例.txt. 在
hmm This is first line of the file.
hmm This is second line of the file.
hmm This is third line of the file.
hmm This is fourth line of the file.
hmm This is fifth line of the file.
hmm This is sixth line of the file.
.
.
.
.
hmm This is n(th) line of the file.
去绝育()
这将恢复由phy(phy)引起的更改。在
>>>myfile.dephy(phy="mum")
这将是示例.txt. 在
^{pr2}$贡献
欢迎拉取请求。对于重大变化,请先打开一个问题,讨论您希望更改的内容。在
许可证
- 项目
标签: