0

I am outputting a file, by populating it with values in certain locations. Any of the names inside the [] brackets are being populated with actual values. But since the values are different in length the format is mess up, how can I do this?

Thank you!

Input file

 type xga_control_type is record
      [NAME]                     : std_logic;              -- [OFFSET]     : [DESCRIPTOIN]

end record xga_control_type;  

Python code

input=open("input.txt","r")
output=open("output.txt","w")

for line in input:
    line=input.readlines()
    if '[OFFSET]' in line:
        line=line.replace('[OFFSET]',register[i]['offset'])

    if '[NAME]' in line:
        line=line.replace('[OFFSET]',register[i]['name'])

    if '[DESCRIPTION]' in line:
        line=line.replace('[DESCRIPTION]',register[i]['description'])  

    output.write(line)

Current output

type xga_control_type is record
      reserved        : std_logic;                      -- 31..27     : 
      force_all_fault_clear        : std_logic;                      -- 26     : Rising edge forces all fault registers to clear
      force_warning        : std_logic;                      -- 25     : Forces AC2 to report a Master Warning
      force_error        : std_logic;                      -- 24     : Forces AC2 to report a Master Error
      reserved        : std_logic;                      -- 23..22     : 
      ref_delay_cnt        : std_logic;                      -- 21..20     : Number of reference commands to delay output by.  Counts in 4us increments

end record xga_control_type;

Desired output

 type xga_control_type is record
      reserved                     : std_logic;                      -- 31..27     : 
      force_all_fault_clear        : std_logic;                      -- 26         : Rising edge forces all fault registers to clear
      force_warning                : std_logic;                      -- 25         : Forces AC2 to report a Master Warning
      force_error                  : std_logic;                      -- 24         : Forces AC2 to report a Master Error
      reserved                     : std_logic;                      -- 23..22     : 
      ref_delay_cnt                : std_logic;                      -- 21..20     : Number of reference commands to delay output by.  Counts in 4us increments     
end record xga_control_type;

4 Answers 4

3

You have two options for string formatting in Python: the % operator and the (preferred) .format() method.

These will allow you to format the text considering number precision, number and string padding and alignment (which is what you seem to need).

Check out the documentation at:

https://docs.python.org/2/library/string.html (Python 2)

https://docs.python.org/3/library/string.html (Python 3)

These examples from those docs are relevant:

>>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'
>>> '{:^30}'.format('centered')
'           centered           '
>>> '{:*^30}'.format('centered')  # use '*' as a fill char
'***********centered***********'
Sign up to request clarification or add additional context in comments.

Comments

0

You can pad to a certain width:

input=open("input.txt","r")
output=open("output.txt","w")

for line in input:
    line=input.readlines()
    if '[OFFSET]' in line:
        line=line.replace('[OFFSET]',register[i]['offset'] + (' ' * (30 - len(register[i]['offset']))))

    if '[NAME]' in line:
        line=line.replace('[OFFSET]',register[i]['name'] + (' ' * (40 - len(register[i]['name'])))

    if '[DESCRIPTION]' in line:
        line=line.replace('[DESCRIPTION]',register[i]['description'])  

    output.write(line)

Don't need to pad the last section though. Feel free to edit the numbers as you see fit.

Comments

0

You should use the .format() syntax like so:

line=line.replace('[OFFSET]', '{0:<40}'.format(register[i]['offset']))

Comments

0

Sounds like you want each column to be the same number of characters (e.g. 20 characters), so you need to pad your string with spaces to make it 20 characters total. The ljust function of a string can do this:

"hello".ljust(20,' ')
>>>'hello               '

Applied to your code, you can do something like this:

line=line.replace('[OFFSET]',register[i]['offset'].ljust(20,' '))

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.