我有一個輸出哪種類型的是str。我想把它寫成一個文件,但它顯示這個錯誤。我嘗試了命令,但它給出了相同的錯誤。writeto_excel錯誤:AttributeError Traceback (most recent call last)<ipython-input-43-b62c7c6bb434> in <module> 7 with open(name) as f: 8 data = f.read().replace("\n", " ")----> 9 data.to_csv("C:\\Users\\frknk\\OneDrive\\Masaüstü\\enron6\\maaail.txt") 10 print(data) 11 except IOError as exc: AttributeError: 'str' object has no attribute 'to_csv'我的代碼:import globimport errnopath =r'C:\Users\frknk\OneDrive\Masaüstü\enron6\emails\*.txt'files = glob.glob(path)for name in files: try: with open(name) as f: data = f.read().replace("\n", " ") data.to_csv("C:\\Users\\frknk\\OneDrive\\Masaüstü\\enron6\\maaail.txt") print(data) except IOError as exc: if exc.errno != errno.EISDIR: raise
2 回答

侃侃爾雅
TA貢獻1801條經(jīng)驗 獲得超16個贊
如果第一個答案沒有保存所有輸出,那是因為它每次都會覆蓋您的文件。
做:
list = []
for name in files:
list.append(name.strip())
with open("file.csv", "w") as file:
for element in list:
file.write(element + "\n")
這應(yīng)該可以解決您的問題!

蝴蝶不菲
TA貢獻1810條經(jīng)驗 獲得超4個贊
字符串沒有方法。to_csv()
嘗試做:
with open("file.csv","w") as file: file.write(data + "\n")
添加回答
舉報
0/150
提交
取消