1

I am writing a program to analyse weblogs. I put the various results into a variable called "output" and write the output at the end to a text file. Below is just one example:

output += "\nFiles and how many times they have been accessed: \n"
for key in accessedFiles.keys():
     output += key + "-> time(s) accessed is: " + "\t" + str(accessedFiles[key]) + " \n"

The output looks like this:

/paperarticles/Nov02-Bushswar.htm-> time(s) accessed is:    1 
/Documents/NinaSimone.htm-> time(s) accessed is:    1 
/cwi.css-> time(s) accessed is:     1

There are similar questions that refer to the print command. I figured out how this would work with f-string but I am not using print in my code. Also, "\t" does not work because there are different legnt of strings. Any idea of how I could align like this? Below is handmade just for visualisation purpose:

/paperarticles/Nov02-Bushswar.htm     time(s) accessed:   1 
/Documents/NinaSimone.htm             time(s) accessed:   1 
/cwi.css->                            time(s) accessed:   1

This is my first question to the forum. I hope I did everything right. Thank you so much for your help. Chris

chootbl
  • 13
  • 3
  • Does this answer your question? [Python: Format output string, right alignment](https://stackoverflow.com/questions/8234445/python-format-output-string-right-alignment) – SuperStormer Apr 03 '20 at 19:36
  • Why do f strings not work? It can exist outside of print. Please share your f string. – minterm Apr 03 '20 at 19:51
  • @ SuperStormer: thanks for the hint. I tried output += '{:>12}{:>12}{:>12}'.format(key + "-> time(s) accessed is: " + str(accessedFiles[key]) + " \n") but this did not work. – chootbl Apr 03 '20 at 19:56

2 Answers2

1

Tabs (\t) only move to the next multiple of x characters (generally 8) so the length of your keys will fall on different tab stops and you would need some way to determine how many \t to place in your output string.

A better alternative would be to determine the width of your longest key and pad the key strings to that maximum in the output.

maxKeyLen = max(map(len,accessedFiles.keys()))
for key,files in accessedFiles.items():
    output += key.ljust(maxKeyLen)+f" -> time(s) accessed is: {files}\n"
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Hi Alain. Thanks, this is indeed a great approach. Sorry for my naiv questions I am new to Python.I tried it and got the following result: output += key.ljust(maxKeyLen)+f" -> time(s) accessed is: {key}\n" ^ SyntaxError: invalid syntax – chootbl Apr 03 '20 at 20:10
  • are you using Python 2.x ? I think the format strings `f"...{..}..."` are not supported in older versions of Python. You could use your original concatenations if that is the case. – Alain T. Apr 03 '20 at 20:30
  • Hi Alain. Yes, sorry about that. Did a downgrade to test something else. No running with 3.7 it perfectly works. Thank you so much for your help. Very appreciated. You saved me hours. It's a great approach with the width of the longest key that I can now use through the entire script. Thx mate – chootbl Apr 03 '20 at 20:43
0

try to use the format method:

"Location: {0:20} Revision {1}".format(Location,Revision)

You will have to figure out the of the format length for each line depending on the length of the label. The User line will need a wider format width than the Location or District lines.

or

You can use expandtabs to specify the tabstop, like this:

print ('Location:'+'10-10-10-10'+'\t'+ 'Revision: 1'.expandtabs(30))
print ('District: Tower'+'\t'+ 'Date: May 16, 2012'.expandtabs(30))

#Output:
Location:10-10-10-10          Revision: 1
District: Tower               Date: May 16, 2012
Diwakar SHARMA
  • 573
  • 7
  • 24