What is StringIO in python used for in reality?

前端 未结 8 1530
囚心锁ツ
囚心锁ツ 2021-01-29 20:46

I am not a pro and I have been scratching my head over understanding what exactly StringIO is used for. I have been looking around the internet for some examples. However, almos

8条回答
  •  野的像风
    2021-01-29 21:20

    I've just used StringIO in practice for two things:

    • To unit-test a script that does a lot of printing, by redirecting sys.stdout to a StringIO instance for easy analysis;
    • To create a guaranteed well-formed XML document (a custom API request) using ElementTree and then write it for sending via a HTTP connection.

    Not that you need StringIO often, but sometimes it's pretty useful.

提交回复
热议问题