Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic conversion of the advanced string formatter from the old style

Is there any automatic way to convert a piece of code from python's old style string formatting (using %) to the new style (using .format)? For example, consider the formatting of a PDB atom specification:

spec = "%-6s%5d %4s%1s%3s %1s%4d%1s   %8.3f%8.3f%8.3f%6.2f%6.2f          %2s%2s"

I've been converting some of these specifications by hand as needed, but this is both error prone, and time-consuming as I have many such specifications.

like image 961
Hooked Avatar asked Mar 22 '23 04:03

Hooked


1 Answers

Use pyupgrade

pyupgrade --py3-plus <filename>

You can convert to f-strings (formatted string literals) instead of .format() with

pyupgrade --py36-plus <filename>

You can install it with

pip install pyupgrade
like image 78
Boris Avatar answered Apr 27 '23 03:04

Boris