1
所以我開始學習python幾個星期前,我想做一個程序,讀取文本文件,並根據文本文件的內容(我的例子將只是數字)它將某些內容輸出到不同的文件。Python - 打印到奇怪的輸出文件
下面是代碼我有:
import tkinter as tk
import time
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
new_doc = []
with open(file_path, encoding='utf-8') as f:
file=f.read()
for line in file:
for x in line:
if x == '0':
new_doc.append(print ('3', end=' '))
elif x == '1':
new_doc.append(print ('6', end=' '))
elif x == '2':
new_doc.append(print ('9', end=' '))
elif x == '3':
new_doc.append(print ('12', end=' '))
elif x == '3':
new_doc.append(print ('15', end=' '))
elif x == '4':
new_doc.append(print ('18', end=' '))
elif x == '5':
new_doc.append(print ('21', end=' '))
elif x == '6':
new_doc.append(print ('24', end=' '))
elif x == '7':
new_doc.append(print ('27', end=' '))
elif x == '8':
new_doc.append(print('30', end=' '))
elif x == '9':
new_doc.append(print ('33', end=' '))
elif x == ' ':
new_doc.append(print ('', end=''))
else:
print ('9 9 9 9 9')
save = filedialog.asksaveasfile(mode='w', defaultextension=".txt")
if save is None:
print('ERROR!')
sys.exit()
str1 = ''.join(str(e) for e in new_doc)
save.write(str1)
save.close()
在命令行腳本的工作原理,例如,123
,輸出6 9 12
。但是當我告訴他寫一個文件時,它只寫None
,如果我告訴他打印一個數字之間的空格,它也會輸出None
。
我的輸出文件基本上是None的。
你確認,你必須寫文件的權利? – Fletchius
從doc.append() – Kajal
中刪除print語句,因爲print()返回None。改用'new_doc.append(「3」)'。 –