1

I have the following python code:

open_file=open("path to a file",'r')
from prettytable import PrettyTable
import csv
table1=PrettyTable(["COLUMN1","COLUMN2","COLUMN3"])
for lines in open_file:
    temp=(lines.split("%")[1])
    get_severity_no=temp.split(":")[0]
    Description=temp.split(":")[1]
    if "3" in get_severity_no:
        GET_DATE=lines.split(";")[0]
        table1.add_row([GET_DATE,get_severity_no,Description])
print(table1)

This code will simply print the desired output in a table format. Sample output shown below:enter image description here

Now I'm calling this script from php. Here is the php code.

<?php

$mystring = system('python get_files.py');
echo $mystring
?>

Now this is the output that I'm getting.

enter image description here

All the formatting has been lost. Could u please let me know what I should do to make this proper? I'm very new to php but comfortable with python.

2 Answers 2

1

Use <pre> tags around your output, as so:

<pre>
 <?php
  $mystring = system('python get_files.py');
  echo $mystring;
 ?>
</pre>

pre stands for "preformatted text", and pre tags render "plain text" exactly how it was entered, which is widely used for displaying code, command output, and other ASCII formatted tables & shapes (like yours) on webpages. Pre tags treat new-line's as <br>'s, conserves repetitive spaces (like you have in your table), treats < and > literally and not as HTML tags, and uses a fixed-width font.

You won't want to use pre tags all the time, because HTML tags inside of it, such as <img>, won't even be rendered. It displays any text literally as it was entered.

Hope this helps,

Sign up to request clarification or add additional context in comments.

3 Comments

It helped. Thanks !!
What's a pre tag??
@soma It renders plain text in readable form, treating new-line's as <br>'s, conserves repetitive spaces (like you have in your table), treats < and > literally and not as HTML tags, and uses a fixed-width font.
1

There's a difference in how the console and the browsers actually format outputs.

The console represents new line with \n while browsers need proper HTML tags <br>. Same thing for spaces. The console will print as many spaces as there are but browsers/HTML removes consecutive spaces (so that only one remains).

So if you want to have exactly the same output, you'll need to convert your line breaks from \n to <br> - you can use the nl2br function for that. Regarding spaces, you'll probably want to convert all spaces to non-breakable spaces, to tell the browsers to render consecutive spaces. You can do that with a regex: preg_replace('/ /', '&nbsp;', $str);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.