【发布时间】: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