【问题标题】:Writing to files in ASCII with Python3, not UTF8使用 Python3 而非 UTF8 写入 ASCII 文件
【发布时间】:2017-10-20 13:26:08
【问题描述】:

我创建了一个包含两个部分的程序。

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

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

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

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

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

如果有人能告诉我我做错了什么,那就太好了。我很想使用file.write('1534') 之类的东西将数据写入文件,而不必使用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

【问题讨论】:

  • 那不是 UTF-8。它看起来更像 UTF-16BE。
  • 好吧,我自己也不确定。感谢您的指正。我对Unicode不太熟悉。我基本上只是将新字符串的格式与旧字符串的格式相同,但编号不同。

标签: python-3.x unicode utf-8


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 谢谢。我会试试。如果我想以读/写方式打开文件,那么“open('somefile', 'r+b')”对吗?
  • @CigEmacs 如果我的答案是您问题的解决方案,如果您能将其标记为已接受的答案,我将不胜感激。
  • 我本来打算先试一试,但在做了一些鸭鸭式之后,我确信它会起作用。非常感谢。
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 2014-04-05
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多