Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python将文本文件转换为html文件_Python_Html_Text - Fatal编程技术网

用python将文本文件转换为html文件

用python将文本文件转换为html文件,python,html,text,Python,Html,Text,我有一个文本文件,其中包含: JavaScript 0 /AA 0 OpenAction 1 AcroForm 0 JBIG2Decode 0 RichMedia 0 Launch 0 Colors>2^24 0 uri

我有一个文本文件,其中包含:

JavaScript              0
/AA                     0
OpenAction              1
AcroForm                0
JBIG2Decode             0
RichMedia               0
Launch                  0
Colors>2^24             0
uri                     0
我编写了以下代码以将文本文件转换为html:

contents = open("C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
    with open("suleiman.html", "w") as e:
        for lines in contents.readlines():
            e.write(lines + "<br>\n")

我应该怎么做才能使文本文件中的内容和两列相同,这是因为HTML解析器会折叠所有空白。有两种方法可以做到这一点(很可能更多)

一种是将其标记为“预格式化文本”,方法是将其放入
标记中

<table>
  <tr><td>Javascript</td><td>0</td></tr>
  ...
</table>
另一个是桌子(这就是制作桌子的目的):


Javascript0
...
手工打字相当繁琐,但很容易从脚本生成。像这样的方法应该会奏效:

contents = open"C:\\Users\\Suleiman JK\\Desktop\\Static_hash\\test","r")
with open("suleiman.html", "w") as e:
    for lines in contents.readlines():
        e.write("<pre>" + lines + "</pre> <br>\n")
contents=open(“C:\\Users\\Suleiman JK\\Desktop\\Static\u hash\\test”,“r”)
以open(“suleiman.html”,“w”)作为e:
e、 写入(“\n”)
from bs4 import BeautifulSoup

soup = BeautifulSoup()
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)

with open('path/to/input/file.txt') as infile:
    for line in infile:
        row = soup.new_tag('tr')
        col1, col2 = line.split()
        for coltext in (col2, col1): # important that you reverse order
            col = soup.new_tag('td')
            col.string = coltext
            row.insert(0, col)
        table.insert(len(table.contents), row)

with open('path/to/output/file.html', 'w') as outfile:
    outfile.write(soup.prettify())
对于contents.readlines()中的行: e、 写入(“%s%s\n”%lines.split())
from bs4 import BeautifulSoup

soup = BeautifulSoup()
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)

with open('path/to/input/file.txt') as infile:
    for line in infile:
        row = soup.new_tag('tr')
        col1, col2 = line.split()
        for coltext in (col2, col1): # important that you reverse order
            col = soup.new_tag('td')
            col.string = coltext
            row.insert(0, col)
        table.insert(len(table.contents), row)

with open('path/to/output/file.html', 'w') as outfile:
    outfile.write(soup.prettify())
e、 写入(“\n”)
from bs4 import BeautifulSoup

soup = BeautifulSoup()
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)

with open('path/to/input/file.txt') as infile:
    for line in infile:
        row = soup.new_tag('tr')
        col1, col2 = line.split()
        for coltext in (col2, col1): # important that you reverse order
            col = soup.new_tag('td')
            col.string = coltext
            row.insert(0, col)
        table.insert(len(table.contents), row)

with open('path/to/output/file.html', 'w') as outfile:
    outfile.write(soup.prettify())

只需将代码更改为包含
标记,以确保文本的格式与原始文本文件中的格式相同

from jinja2 import Template
c = '''<!doctype html>
<html>
<head>
    <title>My Title</title>
</head>
<body>
<table>
   <thead>
       <tr><th>Col 1</th><th>Col 2</th></tr>
   </thead>
   <tbody>
       {% for col1, col2 in lines %}
       <tr><td>{{ col 1}}</td><td>{{ col2 }}</td></tr>
       {% endfor %}
   </tbody>
</table>
</body>
</html>'''

t = Template(c)

lines = []

with open('yourfile.txt', 'r') as f:
    for line in f:
        lines.append(line.split())

with open('results.html', 'w') as f:
    f.write(t.render(lines=lines))
header = '<!doctyle html><html><head><title>My Title</title></head><body>'
body = '<table><thead><tr><th>Col 1</th><th>Col 2</th></tr>'
footer = '</table></body></html>'

with open('input.txt', 'r') as input, open('output.html', 'w') as output:
   output.writeln(header)
   output.writeln(body)
   for line in input:
       col1, col2 = line.rstrip().split()
       output.write('<tr><td>{}</td><td>{}</td></tr>\n'.format(col1, col2))
   output.write(footer)
contents=open“C:\\Users\\Suleiman JK\\Desktop\\Static\u hash\\test”,“r”)
以open(“suleiman.html”,“w”)作为e:
对于contents.readlines()中的行:
e、 写(“+行+”
\n)
from bs4 import BeautifulSoup

soup = BeautifulSoup()
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)

with open('path/to/input/file.txt') as infile:
    for line in infile:
        row = soup.new_tag('tr')
        col1, col2 = line.split()
        for coltext in (col2, col1): # important that you reverse order
            col = soup.new_tag('td')
            col.string = coltext
            row.insert(0, col)
        table.insert(len(table.contents), row)

with open('path/to/output/file.html', 'w') as outfile:
    outfile.write(soup.prettify())

您可以使用像或这样的独立模板库。以下是jinja的一个例子:

<?xml version="1.0" encoding="utf-8"?>
<body>
 <table>
  <p>
   MUTHU PAGE
  </p>
  <tr>
   <td>
    2019/08/19 19:59:25 MUTHUKUMAR_TIME_DATE,line: 118     INFO | Logger object created for: MUTHUKUMAR_APP_USER_SIGNUP_LOG
   </td>
  </tr>
  <tr>
   <td>
    2019/08/19 19:59:25 MUTHUKUMAR_DB_USER_SIGN_UP,line: 48     INFO | ***** User SIGNUP page start *****
   </td>
  </tr>
  <tr>
   <td>
    2019/08/19 19:59:25 MUTHUKUMAR_DB_USER_SIGN_UP,line: 49     INFO | Enter first name: [Alphabet character only allowed, minimum 3 character to maximum 20 chracter]
这是HTML--使用
BeautifulSoup


我已经添加了标题,在这里一行一行地循环,并在和标记上追加每一行,它应该作为没有列的单个表工作。对于col1和col2,不需要使用这些标记(和[为可读性提供了一个空格])

日志:代码段:

穆图页

2019/08/19 19:59:25 MUTHUKUMAR_时间日期,行:118信息记录器 为以下对象创建的对象:MUTHUKUMAR_应用程序_用户_注册_日志2019/08/19 19:59:25 MUTHUKUMAR_DB_用户注册,行:48信息******用户注册页面 开始******2019/08/19 19:59:25 MUTHUKUMAR_DB_用户注册,第49行 信息|输入名字:[仅允许字母字符,至少3个 最多20个字符]

html源页面:

'''


发布所需的输出html的内容应该与上面的文本文件相同,有两列。您的意思是编辑原始文本文件,使每列之间的空格更少吗?原始文本文件的每行之间没有空格,但在html中,行与行之间有一个双空格从第二行删除您的
标记e.write()调用中的文本。
from bs4 import BeautifulSoup

soup = BeautifulSoup()
body = soup.new_tag('body')
soup.insert(0, body)
table = soup.new_tag('table')
body.insert(0, table)

with open('path/to/input/file.txt') as infile:
    for line in infile:
        row = soup.new_tag('tr')
        col1, col2 = line.split()
        for coltext in (col2, col1): # important that you reverse order
            col = soup.new_tag('td')
            col.string = coltext
            row.insert(0, col)
        table.insert(len(table.contents), row)

with open('path/to/output/file.html', 'w') as outfile:
    outfile.write(soup.prettify())