Closed anniehuang921 closed 9 years ago
因為 .... 他不知道
str(x)+ "\n" for x in my_list
這個是甚磨?
可以換成:
my_file.write("\n".join(my_list))
中間直接改會error:
my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "w")
my_file.write("\n".join(my_list)) #
my_file.close()
TypeError: sequence item 0: expected str instance, int found
後來換成:
my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "w")
mylist=list(map(str,my_list)) #
my_file.write("\n".join(mylist)) #
my_file.close()
就可以了~~ ps. In Py3
原本:
嘗試:
不過這樣跑不出來....