使用python修改文本文档

发布于 2024-11-19 17:58:42 字数 219 浏览 2 评论 0原文

假设我有一个 .txt 文档,其中包含 1,000 个姓名。 该文件看起来像这样:

乔恩 简 乔 杰克 杰里米

等。

现在,比方说,我想加上“很蹩脚”。到每一个名字。 所以,我希望列表看起来像这样:

乔恩是跛脚的。 简很跛脚。 乔是瘸子。 杰克是个瘸子。 杰里米是瘸子。

等等。

我如何从命令行使用 python 来做到这一点?

Let's say I had a .txt document with 1,000 names in it.
The document would look like this:

Jon
Jane
Joe
Jack
Jeremy

and, so on.

Now, let's say, I wanted to append "is lame." to each of the names.
So, I'd want the list to look like this:

Jon is lame.
Jane is lame.
Joe is lame.
Jack is lame.
Jeremy is lame.

and, so on.

How would I do this with python from the command line?

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

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

发布评论

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

评论(5

Spring初心 2024-11-26 17:58:42

请参阅http://docs.python.org/tutorial/inputoutput。 html#读和写文件

简而言之:

f = open('my_names_file.txt')
data = f.read()
f.close()
output = ""
for name in data.split(' '):       # This assumes each name is separated 
    output += name + " is lame. "  # by a space and no name contains a space
print output

将输出写回到文件中同样容易。文档中对此进行了很好的解释。

See http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files.

In a nutshell:

f = open('my_names_file.txt')
data = f.read()
f.close()
output = ""
for name in data.split(' '):       # This assumes each name is separated 
    output += name + " is lame. "  # by a space and no name contains a space
print output

It's just as easy to write the output back into a file. It's explained well in the docs.

离不开的别离 2024-11-26 17:58:42
print ' '.join(i + ' is lame.' for i in open(fname).read().split())
print ' '.join(i + ' is lame.' for i in open(fname).read().split())
枕头说它不想醒 2024-11-26 17:58:42

这并不完全是Python特有的,但你明白了......

In a loop parsing each token (name):
1.  Get the name into a string
2.  Append "is lame" or whatever else to the string
3.  Append that string onto a buffer

循环结束后,用新的缓冲区替换你的文档。

This isn't exactly python specific, but you get the idea....

In a loop parsing each token (name):
1.  Get the name into a string
2.  Append "is lame" or whatever else to the string
3.  Append that string onto a buffer

After the loop is over, replace your document with the new buffer.

若无相欠,怎会相见 2024-11-26 17:58:42
>>> s="Jon Jane Joe Jack Jeremy "
>>> s.replace(" ", " is lame. ")
'Jon is lame. Jane is lame. Joe is lame. Jack is lame. Jeremy is lame. '
>>> s="Jon Jane Joe Jack Jeremy "
>>> s.replace(" ", " is lame. ")
'Jon is lame. Jane is lame. Joe is lame. Jack is lame. Jeremy is lame. '
ヅ她的身影、若隐若现 2024-11-26 17:58:42

请在下面找到一个非常基本的示例:

outputLines = []
with open('c:\\file.txt', 'r') as f:
    for line in f:
        if line.strip():
            outputLines.append(' is lame. '.join(line.strip().split()))
with open('c:\\file.txt', 'w') as f:
    for line in outputLines:
        f.write(line+'\n')

Please find below a very basic sample:

outputLines = []
with open('c:\\file.txt', 'r') as f:
    for line in f:
        if line.strip():
            outputLines.append(' is lame. '.join(line.strip().split()))
with open('c:\\file.txt', 'w') as f:
    for line in outputLines:
        f.write(line+'\n')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文