0

I learned all of my python on 3.0 and greater and come to find out the server that I need to put my script on is 2.7.5 which means no f strings. I am having issues converting my script from F strings to .format. Also so I can learn, can you please let me know what I did wrong?

The current working code for 3.10.6

import sys

filename=sys.argv[1]


def reformat_file(filename, desired_length=87, suffix='N'):
    lines = []
    with open(filename, 'r') as fp:
        for line in fp:
            formatted = line
            if filename == 'Bob.txt':
              len(line) < desired_length
              formatted = f"{line:{desired_length}{suffix}}"
              
            else:
                len(line) < desired_length
                formatted = f"{line:{desired_length}}"

            lines.append(formatted)

    with open(filename, 'w') as fp:
        fp.write('\n'.join(lines))

    return

if __name__ == "__main__":
    reformat_file(filename)

I tried doing this in 2.7.5: import sys

filename=sys.argv[1]

def reformat_file(filename, desired_length=87, suffix='N'):
    lines = []
    with open(filename, 'r') as fp:
        for line in fp:
            line = line.strip()
            formatted = line
            if filename == 'Bob.txt':
              len(line) < desired_length
              formatted = ".format({line:{desired_length}}{suffix})"
            else:
               len(line) < desired_length
               formatted = ".format({line:{desired_length}})"               
            lines.append(formatted)

    with open(filename, 'w') as fp:
        fp.write('\n'.join(lines))

    return

if __name__ == "__main__":
    reformat_file(filename)

When I run the 2.7.5 code instead of the desired output I get this:

.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})
.format({line:{desired_length}})

The desired format is to pad the end of data in a file and if the file is Bob.txt add an N at the end of the line.

Example:

1                                    N
111                                  N
111                                  N
1111                                 N
1111                                 N

changing the f string to .format

5
  • It may answer your question: stackoverflow.com/questions/56533498/… Commented Jun 27 at 14:52
  • You aren't using format() correctly pyformat.info Commented Jun 27 at 14:53
  • 2
    len(line) < desired_length What is the purpose of this code? It doesn't actually do anything. Commented Jun 27 at 14:56
  • Josh the original code is a bank file that requires something to reach a certain character length or the payment would fail so the desired length is what is need and right under that code it is say with f string to pad the rest of the line with spaces Commented Jun 27 at 15:09
  • do you means formatted = "{line:{desired_length}}{suffix}".format(line=line, desired_length=desired_length, suffix=suffix)
    – furas
    Commented Jun 27 at 18:15

2 Answers 2

0

Using your current string it could be

formatted = "{line:{desired_length}}{suffix}".format(line=line, desired_length=desired_length, suffix=suffix)

or simpler

formatted = "{0:{1}}{2}".format(line, desired_length, suffix)

or even simpler (but maybe not so readable)

formatted = "{:{}}{}".format(line, desired_length, suffix)

You can also use str.format()

formatted = str.format("{:{}}{}", line, desired_length, suffix)

I almost forgot: you can use dictionary

formatted = "{line:{desired_length}}{suffix}".format(**{"line":line, "desired_length":desired_length, "suffix":suffix})

More details on page: pyformat.info


Full working example

line = "Hello"
desired_length = 20
suffix = 'N'

formatted = "{line:{desired_length}}{suffix}".format(line=line, desired_length=desired_length, suffix=suffix)
print(formatted)

formatted = "{0:{1}}{2}".format(line, desired_length, suffix)
print(formatted)

formatted = "{:{}}{}".format(line, desired_length, suffix)
print(formatted)

formatted = str.format("{:{}}{}", line, desired_length, suffix)
print(formatted)

formatted = "{line:{desired_length}}{suffix}".format(**{"line":line, "desired_length":desired_length, "suffix":suffix})
print(formatted)

Result:

Hello               N
Hello               N
Hello               N
Hello               N
0
line = "line1"
desired_length = 87
suffix = 'N'

old_string = "{%s:%s%s}" % (line,desired_length,suffix)

Output:

{line1:87N}'
1
  • This would not work if I did that the string just says your output, I need it to look like the output example. I got it correct just with the wrong version Commented Jun 27 at 18:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.