How to save text files in python:
Use python's built-in open() class to open the text file, write data to the file, use the write() function, after writing, use the close() function to close and save the text file
The sample code is as follows:
The results are as follows:
Content expansion:
Python3 saves data as a txt file, the specific content is as follows:
f =open("data/model_Weight.txt",'a') #If the file does not exist, the system automatically creates it.'a'Indicates that it can be written to the file continuously, and the original content is retained.
# Write the content later. This mode can be modified ('w+','w','wb'Wait)
f.write("hello,sha") #Write string to file
f.write("\n") #Wrap
if __name__=='__main__':
fw =open("/exercise1/data/query_deal.txt",'w') #The address of the file to be saved
for line inopen("/exercise1/data/query.txt"): #File read
fw.write("\"poiName\":\""+ line.rstrip("\n")+"\"") #Write string to file
# line.rstrip("\n")To remove end-of-line newlines
fw.write("\n") #Wrap
So far, this article on how to save text files in python is introduced. For more related methods of saving text files in python, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. Cn!
Recommended Posts