如何使 lower() 方法适用于读取文件

发布于 2025-01-10 19:09:26 字数 262 浏览 0 评论 0原文

在 python 中,我正在读取一个文件,然后尝试将所有内容转换为小写,但它没有改变。我做错了什么?

fstor = open(txt)
story = fstor.read()
story.lower()

输出(我清理了标点符号和其他标记)

日子一天天过去,最聪明的小猪的房子已经建成了 砖头 他的兄弟们时不时地来看望他,笑着说 你为什么这么努力为什么不来玩但是 顽固的瓦工猪只说不 我将首先完成我的房子 它必须坚固耐用 然后我会

In python, I am having a file read then trying to convert everything to lower case but it isn't changing. What am I doing wrong?

fstor = open(txt)
story = fstor.read()
story.lower()

OUTPUT ( I cleaned up punctuations and other marks )

The days went by and the wisest little pig's house took shape brick by
brick From time to time his brothers visited him saying with a chuckle
Why are you working so hard Why don't you come and play But the
stubborn bricklayer pig just said no
I shall finish my house first It must be solid and sturdy And then I'll

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2025-01-17 19:09:26

字符串在 python 中是不可变

fstor = open("file.txt")
story = fstor.read()
#story.lower() #strings are immutable in python
data = story.lower() #return a new string with lowercase
print(data)

输出:

the days went by and the wisest little pig's house took shape brick by brick from time to time his brothers visited him saying with a chuckle why are you working so hard why don't you come and play but the stubborn bricklayer pig just said no i shall finish my house first it must be solid and sturdy and then i'll

将数据写入另一个文件

with open("file.txt") as fstor:
    story = fstor.read()
    data = story.lower() #return a new string with lowercase

    with open('another_file.txt','w') as afile:
        afile.write(data)

Strings are immutable in python

fstor = open("file.txt")
story = fstor.read()
#story.lower() #strings are immutable in python
data = story.lower() #return a new string with lowercase
print(data)

Output:

the days went by and the wisest little pig's house took shape brick by brick from time to time his brothers visited him saying with a chuckle why are you working so hard why don't you come and play but the stubborn bricklayer pig just said no i shall finish my house first it must be solid and sturdy and then i'll

Write the data to another file

with open("file.txt") as fstor:
    story = fstor.read()
    data = story.lower() #return a new string with lowercase

    with open('another_file.txt','w') as afile:
        afile.write(data)
鹿港巷口少年归 2025-01-17 19:09:26
with open('directory_path.txt', 'r') as f: 
    text = f.read() 
 
text = text.lower()
print(text)

这段代码有效:)

with open('directory_path.txt', 'r') as f: 
    text = f.read() 
 
text = text.lower()
print(text)

works this code :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文