使用 Python3 而不是 UTF8 以 ASCII 格式写入文件 [英] Writing to files in ASCII with Python3, not UTF8

查看:48
本文介绍了使用 Python3 而不是 UTF8 以 ASCII 格式写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由两个部分创建的程序.

I have a program that I created with two sections.

第一个以这种格式复制文件名中间带有整数的文本文件.

The first one copies a text file with an integer in the middle of the file name in this format.

file = "Filename" + "str(int)" + ".txt" 

用户可以创建任意数量的文件副本.

the user can create as many copies of the file that they would like.

程序的第二部分是我遇到的问题.文件最底部有一个整数,与文件名中的整数相对应.第一部分完成后,我以 "r+" 读/写格式一次打开每个文件.所以我可以 file.seek(1000) 找到整数在文件中的位置.

The second part of the program is what I am having the problem with. There is an integer at the very bottom of the file that is to correspond with the integer in the file name. After the first part is done, I open each file one at a time in "r+" read/write format. So I can file.seek(1000) to about where the integer is in the file.

现在我认为下一部分应该很容易.我应该只需要在此处将 str(int) 写入文件即可.但这并不容易.像在家里的 Linux 中那样,它工作得很好,但在 Windows 上工作却被证明是困难的.在 file.seek(1000) 使用 Unicode UTF-8 写入文件后,我最终不得不做的事情.我用这个程序其余部分的代码片段完成了这个.我将记录下来,以便能够理解发生了什么.不必用 Unicode 编写它,我希望能够用古老的常规英文 ASCII 字符编写它.最终,该程序将被扩展以在每个文件的底部包含更多数据.必须以 Unicode 编写数据将使事情变得极其困难.如果我只是写入数据而不将其转换为 Unicode,这就是结果.这个字符串应该是 #2 =1534,而是 #2 =ㄠ㌵433.

Now in my opinion the next part should be easy. I should just simply have to write str(int) into the file right here. But it wasn't that easy. It worked just fine doing it like that in Linux at home, but at work on Windows it proved difficult. What I ended up having to do after file.seek(1000) is write to the file using Unicode UTF-8. I accomplished this with this code snippet of the rest of the program. I will document it so that it is able to be understood what is going on. Instead of having to write this in Unicode, I would love to be able to write this in good old regular English ASCII characters. Eventually this program will be expanded to include a lot more data at the bottom of each file. Having to write the data in Unicode is going to make things extremely difficult. If I just write the data without turning it into Unicode this is the result. This string is supposed to say #2 =1534, instead it says #2 =ㄠ㌵433.

如果有人能告诉我我做错了什么,那就太好了.我很想使用诸如 file.write('1534') 之类的东西将数据写入文件,而不必在 Unicode UTF-8 中进行.

If someone can show me what I am doing wrong that would be great. I would love to just use something like file.write('1534') to write the data to the file instead of having to do it in Unicode UTF-8.

while a1 < d1 :
    file = "file" + str(a1) + ".par"
    f = open(file, "r+")
    f.seek(1011)
    data = f.read()  #reads the data from that point in the file into a variable.
    numList= list(str(a1)) # "a1" is the integer in the file name. I had to turn the integer into a list to accomplish the next task.
    replaceData = '\x00' + numList[0] + '\x00' + numList[1] + '\x00' + numList[2] + '\x00' + numList[3] + '\x00' #This line turns the integer into Utf 8 Unicode. I am by no means a Unicode expert.
    currentData = data #probably didn't need to be done now that I'm looking at this.
    data = data.replace(currentData, replaceData) #replaces the Utf 8 string in the "data" variable with the new Utf 8 string in "replaceData."
    f.seek(1011) # Return to where I need to be in the file to write the data.
    f.write(data) # Write the new Unicode data to the file
    f.close() #close the file
    f.close() #make sure the file is closed (sometimes it seems that this fails in Windows.)
    a1 += 1 #advances the integer, and then return to the top of the loop

推荐答案

这是一个以 ASCII 格式写入文件的示例.您需要以字节方式打开文件,对字符串使用 .encode 方法是获得您想要的最终结果的便捷方式.

This is an example of writing to a file in ASCII. You need to open the file in byte mode, and using the .encode method for strings is a convenient way to get the end result you want.

s = '12345'
ascii = s.encode('ascii')
with open('somefile', 'wb') as f:
    f.write(ascii)

如果文件已经存在,你显然也可以在你的情况下以 rb+(读写字节模式)打开.

You can obviously also open in rb+ (read and write byte mode) in your case if the file already exists.

with open('somefile', 'rb+') as f:
    existing = f.read()
    f.write(b'ascii without encoding!')

您也可以只传递带有 b 前缀的字符串文字,它们将使用 ascii 进行编码,如第二个示例所示.

You can also just pass string literals with the b prefix, and they will be encoded with ascii as shown in the second example.

这篇关于使用 Python3 而不是 UTF8 以 ASCII 格式写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆