I couldn't figure it out with the Python documentation, so maybe i can ask it here:
import StringIO
line = StringIO.StringIO()
line.write('Hello World')
Is there any method i can use, that will do what line[1:]
would do on a string,
so line.getvalue()
will return ello World
?
Thanks.
Python provides various inbuilt functions, Slicing() is one of them. If we wish to delete the first character or some other char from the python string, we can erase that character using the slicing method and then get the resultant string excluding the first character.
Use the . strip() method to remove whitespace and characters from the beginning and the end of a string. Use the . lstrip() method to remove whitespace and characters only from the beginning of a string.
Alternatively, you can use the str. lstrip() and str. rstrip() methods to remove characters from the start and end of the string.
You can use Python's regular expressions to remove the first n characters from a string, using re's . sub() method. This is accomplished by passing in a wildcard character and limiting the substitution to a single substitution.
I can't figure out how to do it with line.getvalue
, but you can use StringIO objects like normal file objects. Just seek to byte 1 and read as you normally would.
>>> import StringIO
>>> line = StringIO.StringIO()
>>> line.write("Hello World")
>>> line.seek(0)
>>> print line.getvalue()
Hello World
>>> line.seek(1)
>>> print line.getvalue()
Hello World
>>> line.seek(1)
>>> print next(line)
ello World
>>> line.seek(1)
>>> print line.read()
ello World
The StringIO getvalue() function return the content as string, so this could be work:
content = line.getvalue()
print content[1:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With