You need to use the output of f.read().
string = f.read()
I think your confusion is that f will be turned into a string just by calling its method .read(), but that's not the case. I don't think it's even possible for builtins to do that.
For reference, _io.TextIOWrapper is the class of an open text file. See the documentation for io.TextIOWrapper.
By the way, best practice is to use a with-statement for opening files:
with open("document.txt", "r", encoding='utf-8-sig') as f:
string = f.read()
Answer from wjandrea on Stack OverflowYou need to use the output of f.read().
string = f.read()
I think your confusion is that f will be turned into a string just by calling its method .read(), but that's not the case. I don't think it's even possible for builtins to do that.
For reference, _io.TextIOWrapper is the class of an open text file. See the documentation for io.TextIOWrapper.
By the way, best practice is to use a with-statement for opening files:
with open("document.txt", "r", encoding='utf-8-sig') as f:
string = f.read()
This is good:
with open(file, 'r', encoding='utf-8-sig') as f:
data = f.read()
This is not good:
with open(file, 'r', encoding='utf-8-sig') as file:
data = file.read()
Python Help. How to write _io.TextIoWrapper variable type to text file? Or convert it to a string?
covert str to io.TextIOWrapper
Getting io.TextIOWrapper instead of string when using --patterns - usage - Prodigy Support
How to get the contents of python object <class '_io.TextIOWrapper'> as a string when using PIPE of subprocess? - Stack Overflow
Hello!
I am grabbing a some data off of an API. I then am trying to take the variable in which I have stored the gathered data and write it to a text file.
When I define the type my variable is it shows
<class '_io.TextIOWrapper'>
I have tried to figure out how to convert it to a string (as that what the error yells at me for) but I cannot find the function.
import time, json, requests
def btstamp():
bitStampTick = requests.get('https://www.bitstamp.net/api/ticker/')
return bitStampTick.json()['last']
bitstampprice = (btstamp())
print (bitstampprice)
bitstampprice = open("testwrite1.txt", "w+")
print (type(bitstampprice))
bitstampprice.write(bitstampprice)
bitstampprice.close()Here is the error
C:\TradingBot>export.py
6456.95
<class '_io.TextIOWrapper'>
Traceback (most recent call last):
File "C:\TradingBot\export.py", line 15, in <module>
bitstampprice.write(bitstampprice)
TypeError: write() argument must be str, not _io.TextIOWrapper
C:\TradingBot>I have spent a good amount of time doing this and its getting late. Any help would be greatly appreciated.
I go into some info about _io.TextIOWrapper and the info is a bit advance for me.
Why is my data being returned in this type?
How come it is not a integer as it is shown when I "print" the variable?
Thanks
Aluad
Hello everyone, for the purpose of my project I need to convert a string (an email) to io.TextIOWrapper format so I can send the email. Does anyone know how I can convert? Thank you
I am writing function to help me printing data in text files with different types. in my code for function def write(self, file:io.TextIOWrapper): i want to specify type of my file variable to improve my documentation and code readability. Any idea how to do this?
class Node:
Id: int
X: float
Y: float
Z: float
def __init__(self, Id: int, x: float, y: float, z: float):
self.Id = int(Id)
self.X = float(x)
self.Y = float(y)
self.Z = float(z)
def write(self, file:io.TextIOWrapper):
file.write(f"{self.Id},{self.X:0.4f},{self.Y:0.4f},{self.Z:0.4f};\n")
def __str__(self):
return f"{self.Id}:{self.X},{self.Y},{self.Z}"I'm supposed to create a dictionary from the values in a file and search for 'Polly' and remove it if it's in the file,
I kept getting errors so I'm just trying to get it to print the value just so I can see what I'm doing wrong and even at the most simple level I keep getting errors,
I'm in my first semester so this is all still new to me
employees = {}
employees = open('dictionary_values.txt', 'r')
print(employees['Polly'])
Traceback (most recent call last):
File "main.py", line 3, in <module>
print(employees['Polly'])
TypeError: '_io.TextIOWrapper' object is not subscriptable