如何在 Python 中打印 .txt 文件的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18256363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print the content of a .txt file in Python?
提问by Courtney
I'm very new to programming (obviously) and really advanced computer stuff in general. I've only have basic computer knowledge, so I decided I wanted to learn more. Thus I'm teaching myself (through videos and ebooks) how to program.
我对编程(显然)和真正高级的计算机东西非常陌生。我只有基本的计算机知识,所以我决定要学习更多。因此,我正在自学(通过视频和电子书)如何编程。
Anyways, I'm working on a piece of code that will open a file, print out the contents on the screen, ask you if you want to edit/delete/etc the contents, do it, and then re-print out the results and ask you for confirmation to save.
无论如何,我正在编写一段代码,它将打开一个文件,在屏幕上打印出内容,询问您是否要编辑/删除/等内容,然后重新打印结果并要求您确认保存。
I'm stuck at the printing the contents of the file. I don't know what command to use to do this. I've tried typing in several commands previously but here is the latest I've tried and no the code isn't complete:
我被困在打印文件的内容上。我不知道使用什么命令来执行此操作。我之前尝试过输入几个命令,但这是我尝试过的最新命令,没有代码不完整:
from sys import argv
script, filename = argv
print "Who are you?"
name = raw_input()
print "What file are you looking for today?"
file = raw_input()
print (file)
print "Ok then, here's the file you wanted."
print "Would you like to delete the contents? Yes or No?"
I'm trying to write these practice codes to include as much as I've learned thus far. Also I'm working on Ubuntu 13.04 and Python 2.7.4 if that makes any difference. Thanks for any help thus far :)
我正在尝试编写这些练习代码,以包含迄今为止我所学到的尽可能多的内容。此外,我正在 Ubuntu 13.04 和 Python 2.7.4 上工作,如果这有什么不同的话。感谢您到目前为止的任何帮助:)
采纳答案by Greg
Opening a file in python for reading is easy:
在 python 中打开文件进行读取很容易:
f = open('example.txt', 'r')
To get everything in the file, just use read()
要获取文件中的所有内容,只需使用 read()
file_contents = f.read()
And to print the contents, just do:
要打印内容,只需执行以下操作:
print (file_contents)
Don't forget to close the file when you're done.
完成后不要忘记关闭文件。
f.close()
回答by Courtney
Just do this:
只需这样做:
>>> with open("path/to/file") as f: # The with keyword automatically closes the file when you are done
... print f.read()
This will print the file in the terminal.
这将在终端中打印文件。
回答by Stephan
with open("filename.txt", "w+") as file:
for line in file:
print line
This with
statement automatically opens and closes it for you and you can iterate over the lines of the file with a simple for
loop
此with
语句会自动为您打开和关闭它,您可以使用简单的for
循环遍历文件的各行
回答by Noel Evans
This will give you the contents of a file separated, line-by-line in a list:
这将为您提供列表中逐行分隔的文件内容:
with open('xyz.txt') as f_obj:
f_obj.readlines()
回答by SamuraiT
to input a file:
输入文件:
fin = open(filename) #filename should be a string type: e.g filename = 'file.txt'
to output this file you can do:
要输出此文件,您可以执行以下操作:
for element in fin:
print element
if the elements are a string you'd better add this before print:
如果元素是一个字符串,你最好在打印前添加:
element = element.strip()
strip()
remove notations like this: /n
strip()
删除这样的符号: /n
回答by Ninja420
print ''.join(file('example.txt'))
print ''.join(file('example.txt'))
回答by Giovanni G. PY
How to read and print the content of a txt file
如何读取和打印txt文件的内容
Assume you got a file called file.txt that you want to read in a program and the content is this:
假设你有一个名为 file.txt 的文件,你想在程序中读取它,内容是这样的:
this is the content of the file
with open you can read it and
then with a loop you can print it
on the screen. Using enconding='utf-8'
you avoid some strange convertions of
caracters. With strip(), you avoid printing
an empty line between each (not empty) line
You can read this content: write the following script in notepad:
您可以阅读此内容:在记事本中编写以下脚本:
with open("file.txt", "r", encoding="utf-8") as file:
for line in file:
print(line.strip())
save it as readfile.py for example, in the same folder of the txt file.
例如,将其保存为 readfile.py,在 txt 文件的同一文件夹中。
Then you run it (shift + right click of the mouse and select the prompt from the contextual menu) writing in the prompt:
然后运行它(shift + 右键单击鼠标并从上下文菜单中选择提示)在提示中写入:
C:\examples> python readfile.py
C:\examples> python readfile.py
You should get this. Play attention to the word, they have to be written just as you see them and to the indentation. It is important in python. Use always the same indentation in each file (4 spaces are good).
你应该得到这个。注意这个词,它们必须像你看到的那样书写,并注意缩进。这在python中很重要。在每个文件中始终使用相同的缩进(4 个空格是好的)。
output
输出
this is the content of the file
with open you can read it and
then with a loop you can print it
on the screen. Using enconding='utf-8'
you avoid some strange convertions of
caracters. With strip(), you avoid printing
an empty line between each (not empty) line