简体   繁体   English

将数据保存到文本文件中

[英]Saving data into a text file

def ConvertFile():
  FileNameIn = 'Hexdata.dat'
  HexFile = open(FileNameIn, 'r')
  for Line in HexFile:
    print (Line)
    print (Binary(Line))
  HexFile.close()

So far I have that, which, when the program is run, converts the Hexidecimal number in the file to binary.到目前为止,我已经有了,当程序运行时,它将文件中的十六进制数转换为二进制数。 This is in a file called Hexdata.dat这是在一个名为 Hexdata.dat 的文件中

What I want to do is then save the binary output into a file called Binarydata.dat我想要做的是将二进制 output 保存到一个名为 Binarydata.dat 的文件中

How would I approach this in code?我将如何在代码中处理这个问题? Be aware I'm new with Python and haven't covered this properly.请注意,我是 Python 的新手,并没有正确介绍这一点。 I've tried different bits of code but they've all been unsuccessful, as really, they're all guesses.我尝试了不同的代码,但都没有成功,实际上,它们都是猜测。

I'm not asking you to solve the problem for me, but more asking how I would save the output of a program into a new text file.我不是要你帮我解决问题,而是问我如何将程序的 output 保存到一个新的文本文件中。

You're already most of the way there.你已经完成了大部分工作。 You already know how to open a file for reading:您已经知道如何打开文件进行阅读:

HexFile = open(FileNameIn, 'r')

The 'r' there means "open for reading".那里的'r'意思是“开放阅读”。 If you look at the documentation for the open function , you will see that replacing the r with a w will open a file for writing:如果您查看open function 的文档,您将看到用w替换r将打开一个文件进行写入:

OutputFile = open(FileNameOut, 'w')

And then you can send output to it like this:然后你可以像这样发送 output 给它:

print >>OutputFile, "Something to print"

Or use the write method on the file object:或者对文件object使用write方法:

OutputFile.write("Something to print\n")

You are currently opening the file in reading mode, so in order to write to the file, you would want to open the file with the buffering mode as ('w') .您当前正在以读取模式打开文件,因此为了写入文件,您需要以('w')的缓冲模式打开文件。 Quote from: http://docs.python.org .引用自: http://docs.python.org You can do so easily by replacing your 'r' with 'w' .您可以通过将'r'替换为'w'来轻松做到这一点。

'w' for writing (truncating the file if it already exists

For more reference see open(name[, mode[, buffering]])有关更多参考,请参阅open(name[, mode[, buffering]])

# the file name
FileNameIn = 'Hexdata.dat'

# create a file object: open it with "write" mode
HexFile = open(FileNameIn,"w")

for line in HexFile:
    HexFile.write(Binary(line))

HexFile.close()

Have you tried using open('Binarydata.dat', 'w') for writing to the file?您是否尝试过使用 open('Binarydata.dat', 'w') 写入文件? There are plenty of ways to write to a file, most of which can be found here: http://docs.python.org/tutorial/inputoutput.html有很多方法可以写入文件,其中大部分可以在这里找到: http://docs.python.org/tutorial/inputoutput.html

Read the documentation of the open function (to open the file in write mode) andFile Objects (to write information to the opened file).阅读open function (以写入模式打开文件)和File Objects (将信息写入打开的文件)的文档。

You have to have 2 files in this script.此脚本中必须有 2 个文件。 The one you're reading from and the one you're writing to.您正在阅读的那个和您正在写的那个。 Use the option wb (write binary) when open ing the file you are going to write into.打开要写入的文件时使用选项wb (写入二进制文件)。 These two links should help a beginner with little or no Python knowledge complete your exercise: Intro to File Objects and Tutorial on File I/O .这两个链接应该可以帮助 Python 知识很少或没有知识的初学者完成您的练习: 文件对象介绍文件 I/O 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM