-1
我目前正在編寫一個代碼,讀取一個包含「您贏了100嗎?是!」的文件,將每個字符轉換爲它的基本10位ASCII值,然後將基本10個ASCII值轉換爲用戶輸入的基數2-9。所有不是alphaNumeric的字符都被轉換爲「..」如何格式化寫入輸出文件Python 3的內容?
我不允許使用內置的python函數在鹼基之間進行轉換,所以我編寫了自己的以新鹼基爲基礎的10位ASCII值並將所有餘數收集到列表中並反轉列表,以便新號碼按正確的順序排列。
我的代碼然後應該寫出新的數字和「..」到出文件,但我無法弄清楚如何格式化出列文件的列表。當我提交代碼時,它說「builtins.TypeError:write()不帶關鍵字參數」。如何將剩餘列表寫入輸出文件並能夠將其格式化爲單個字符串,而不用括號和逗號?謝謝!
infile = open("myFile.txt" , "r")
outfile = open("x_myFile.txt" , "w")
userBase = int(inputBase())
for line in infile:
for ch in line:
if ch.isalpha() or ch.isdigit():
value = int(ord(ch))
remainders = list()
while value>0:
remainders.append(value % userBase)
value//=userBase
remainders = remainders[::-1]
outfile.write(*remainders, sep='')
else:
outfile.write("..")
infile.close()
outfile.close()
'.write'方法只需要一個參數:您要寫入的文本或字節字符串。但是,您也可以使用'print'函數和'file' arg來寫入文件。 –