C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Write: We call the write method to output data to our stream. It is just stored in memory. A print() call also works.
Getvalue: The getvalue method returns a string containing the data written to the in-memory buffer.
Close: We call the close method to prevent further writes to our StringIO stream. This is good housekeeping.
Python program that uses StringIO
import io
out = io.StringIO()
# Print these string values in a loop.
for i in range(0, 100):
out.write("Value = ")
out.write(str(i))
out.write(" ")
# Get string and display first 20 character.
data = out.getvalue()
print(data[0:20])
out.close()
Output
Value = 0 Value = 1
Python program that uses print
import io
out = io.StringIO()
# Print to StringIO stream, no end char.
print("Hello", file=out, end="")
# Print string contents to console.
print(out.getvalue())
Output
Hello
PyPy: In the PyPy implementation, StringIO was nearly twice as fast as a string append.
Note: The if-check in the benchmark loops is just a simple sanity check. The strings must begin with the letter V.
IfPython program that benchmarks StringIO, string concats
import io
import time
print(time.time())
# 1. Use StringIO.
for x in range(0, 10000):
out = io.StringIO()
for i in range(0, 100):
out.write("Value = ")
out.write(str(i))
out.write(" ")
# Get string.
contents = out.getvalue()
out.close()
# Test first letter.
if contents[0] != 'V':
raise Error
print(time.time())
# 2. Use string appends.
for x in range(0, 10000):
data = ""
for i in range(0, 100):
data += "Value = "
data += str(i)
data += " "
# Test first letter.
if data[0] != 'V':
raise Error
print(time.time())
Output
1404346870.027 [PyPy3 2.3.1]
1404346870.286 StringIO: 0.259 s
1404346870.773 Concat: 0.487 s
1404346852.222672 [Python 3.3]
1404346853.343738 StringIO: 1.121 s
1404346854.814824 Concat: 1.471 s