python中向文本每行行尾添加字符
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中向文本每行行尾添加字符相关的知识,希望对你有一定的参考价值。
如1.txt文件的内容是:
1
2
3
用python如何实现改成
1 0
2 1
3 2
file = open(file_path, "r")
content = file.readlines()
file.close()
return content
def write_file(content, file_path="abc1.txt"):
file = open(file_path, "w")
file.writelines(content)
file.flush()
file.close()
if __name__ == "__main__":
content = read_file()
new_content = list()
for c in content:
c = c.strip()
new_content.append(c + str(int(c) - 1) + "\r\n")
write_file(new_content) 参考技术B f = open('1.txt')
o = open('2.txt', 'w+')
for line in f:
o.write(line)
o.write(你要加的数据) 参考技术C s = open('1.txt','r').readlines() #读取成每一行构成的列表
out=open('out.txt','w') #打开一个输出文件用以写入
n=len(s)
for i in range(n):
out.write("%s %s"%(s[i],[i])) #向输出文件中写入:第i行、数字i
out.close() 参考技术D 看你要求,如果是可以新建一个文本的话,上面的兄弟们答案都可以,如果是原文档修改的话,看看我以前回得这个吧,有代码可以直接运行 http://zhidao.baidu.com/question/433093220.html?oldq=1
如何在 python 中向 HTML 添加类和样式表?
【中文标题】如何在 python 中向 HTML 添加类和样式表?【英文标题】:How to add classes and stylesheets to HTML in python? 【发布时间】:2021-06-29 00:45:27 【问题描述】:我正在从 API 中抓取数据并使用 html_file.write 方法将它们输入到 html 文件中。我想将文本居中并添加边框和字体等。但是当我将例如 .content 类添加到
html_content=f"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>"+"""
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Questrial&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<style>
.content
width: 50%;
margin: auto;
background: white;
padding: 10px;
img
max-width: 100%;
body background-color: LightGray;
h1 color: red;
h2 color: red;
</style>
"""+f"</head><body><div class="container"><h1><h1><font size='6'><strong>Q</strong></font></h1><p>content2modified</p><hr><h1><font size='6'><strong>A</strong></font></h1><p>contentmodified</p> </body></div> </html>"
with open("index.html","w",encoding='utf-8') as html_file:
html_file.write(html_content)
print("Success")
await message.author.send(file=discord.File('index.html'))
【问题讨论】:
【参考方案1】:尝试在 f 字符串中复制括号。 '' -> '' 和 '' -> ''
【讨论】:
不,容器上的语法仍然无效【参考方案2】:想通了:
html_content=f"<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>"+"""
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Questrial&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<style>
h1
font-family: 'Questrial', sans-serif;
p
font-family: 'Questrial', sans-serif;
.content
width: 100%;
margin: auto;
background: white;
padding: 10px;
img
max-width: 100%;
body background-color: LightGray;
h1 color: red;
</style>
</head><body><div class="container"><div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<center><strong>Powered by Kurdi. Join our Discord server: https://discord.gg/4UNjM6Cy </strong>
</div><div class="content"">"""+f"<h1><h1><font size='6'><strong>Q</strong></font></h1><p>r5</p><hr><h1><font size='6'><strong>A</strong></font></h1><p>anshtml</p> </body></div> </html>"
【讨论】:
以上是关于python中向文本每行行尾添加字符的主要内容,如果未能解决你的问题,请参考以下文章