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}$

贡献

欢迎拉取请求。对于重大变化,请先打开一个问题,讨论您希望更改的内容。在

许可证

MIT

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
javaelk:如何在Kibana中按异常类对stacktrace进行分组   java两个组件使用MigLayout相互重叠   java Hibernate标准获取关联实体的列表,而不是父实体的列表   从Java关闭另一个应用程序,但不是由当前Java应用程序启动   java使用OGNL获取参数   java如何在SSLEngine中启用密码TLS\U DHE\U RSA\U和\U AES\U 256\U GCM\U SHA384   在Java中有效地比较两个列表<Object[]>   java杀死挂起的线程   在java中从指定模式前后的字符串中提取子字符串   存储整数的java HashMap替代方案   java如何使用LibGDX加载特定于语言的资产?   java如何使用JSON响应从维基百科读取结构化数据   java无法连接到Spark Master:原因是:[已解除关联]   java如何配置Elastic beanstalk classic负载平衡器以使用CLI终止HTTPS   java筛网中的奥斯汀大于int   java PircBot在每个用户上迭代一组命令   java将带有Jackson的hashmap编组为XML的错误结构   testng中的java设置testfailure给出了正回报   java如何在IntelliJ中正确配置Eclipse项目?