0
我是一個完全編程的新手。我試圖在txt文件中解析和格式化'破損'行(文件中的流氓文件而不是\ cr \ lf窗口格式)。使用python 3.4並閱讀這些類型的帖子,我已經設法讀取源文件,並創建一個文件,其中只有破損的行,所有的lf都被刪除,所以它的一個長行。現在我需要讀取行並計算這種格式的分隔符'< |'',在第36行之後添加一個換行符,然後繼續計算下一個36行並添加一個換行符等。我嘗試了幾個不同的東西,但有因爲我不確定是否需要.tell()然後使用.seek()來插入\ n。有關如何在第36分隔符後插入換行符的任何建議?使用python修復txt文件損壞的行
my_count = 36 # define the number of delimiters to count
LineNumber = 1 # define line counter
FileName = 'Broken_Registrations.txt' # variable to define filename
target = open('Target.txt','w',encoding='utf-8') # open a file to write fixed lines
with open(FileName,encoding="utf8") as file:
for line in file: # open file read
cnt=line.count('<|>') # count delimiters
if cnt == mycount: # count until mycount then
target.write(line).append("\n") # write line and append new line char
print('DONE!') # let me know when you finished
target.close() # close the file opened outside of the with