感觉可以,但没必要
```python
import json
class Read(str):
def __new__(cls, filepath):
with open(filepath, 'r', encoding='utf-8') as f:
# return super(Read, cls).__new__(cls, f.read())
return Read.__make_str(f.read())
@
classmethod def __make_str(cls, value):
return super(Read, cls).__new__(cls, value)
def loads_to_json(self):
return json.loads(self)
def strip(self, char = " "):
return Read.__make_str(super().strip(char))
def replace(self, *args):
return Read.__make_str(super().replace(*args))
# use as str
file_cOntent= Read("file.txt")
print("file_content: " + file_content.strip())
print(json.loads(file_content))
# chaining
jsOnContent= Read("file.txt").replace("hell0", "hello").loads_to_json()
print(jsonContent.get("message"))
```
output:
```shell
Downloads python3
v2ex.pyfile_content: {"message" : "hell0, world!"}
{'message': 'hell0, world!'}
hello, world!
```