0

I'm fairly new to python and made something that had this output: (The text is in a csv file so so: 1,A 2,B 3,C etc)

Number        Letter

1             A
2             B
3             C
26            Z

Unfortunately, I spent a good amount of time making it using a complicated method in which I manually made spaces like this: Updated Code rn

fx = int(input('Number?\n'))
f=open('nums.txt','r')
lines=f.readlines()
line = lines[fx - 1]
with open('nums.txt','r') as f:
    for i, line in enumerate(f):
        if i >= 5: 
            break
        NUM, LTR, SMB = line.rsplit(',', 1)
        print(NUM.ljust(13) + LTR.ljust(13) + SMB)

How do I get it to make 3 columns? Right now it comes up with a

ValueError: not enough values to unpack (expected 3, got 2) 

So is there a simpler method of achieving this that doesn't move the strings around like this:

Number        Letter

1             A
2             B
3             C
26             Z #< string moves with spaces.
2
  • 1
    use format... Commented Feb 4, 2017 at 21:30
  • If you're looking for fixed-width columns, don't you just want something like '{:<13}{:<13}'.format(26, 'Z') Commented Feb 4, 2017 at 21:30

2 Answers 2

2

For simple alignment, you can use ljust or rjust. There is also no need to read the entire file for each line you want to process:

with open('numberletter','r') as f:
    for i, line in enumerate(f):
        if i >= 5: 
            break
        number, letter = line.rsplit(',', 1)
        print(number.ljust(13) + letter)

For more complex output formatting, look at str.format() and the formatting syntax

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

5 Comments

What if I wanted to have 3 columns for example: Numbers Letters Symbols
You would apply ljust to the second column, as well. Or better, just use rjust on every column. Right alignment looks better in most cases, anyway ;-)
So would it just be NUM, LTR, SMB = line.rsplit(',', 1) print(NUM.ljust(13) + LTR.ljust(13) + SMB)
Yeah, but NUM, LTR, SMB = line.rsplit(',', 2), since you need two splits now...
Do you know how I can also add a string along with the file? Right now everytime I try do this: print(NUM.ljust(13) + LTR.ljust(13) + SMB + 'test') the 'test' string moves to the first character of the output like this: test 26 z instead of 26 test z
-1

You can use sys module for that.

import sys
a=[1,"A"]
sys.stdout.write("%-6s %-50s " % (a[0],a[1]))

4 Comments

What does that have to do with sys apart from that you happen to be able to write to sys.stdout? The reason why this works has nothing to do with sys and everything with the (deprecated) string formatting operator %.
@L3viathan I think he put sys in there because the print function keeps printing new lines after each output and I requested output with no newlines.
@Yaya print(s, end='') does the same with less effort
For no newlines, add a end='' argument to your call to print.

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.