Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write python file stream

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "python file stream reading and writing how to operate", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "python file stream reading and writing how to operate" it!

The read and write of the file stream reads the txt file that saves the data as an array

Use try for exception discovery and use while to detect the end of the file for reading

File_to_read = raw_input ("Enter file name of tests (empty string to end program):") try: infile = open (file_to_read,'r') while file_to_read! = "": file_to_write = raw_input ("Enter output file name (.csv will be appended to it):") file_to_write = file_to_write + ".csv" outfile = open (file_to_write "w") readings = (infile.readline ()) print readings while readings! = 0: global count readings = int (readings) minimum = (infile.readline ()) maximum = (infile.readline ())

Use for to traverse each row read for one-time read and input

The data read by the program called below is

Result = list () with open ('.. / test/parameter.txt') as f: for line in f.readlines (): temp = list () # iterate through each row of elements one by one Convert it to the corresponding data b = line.strip (",] ["). Split (',') if (len (b) > = 5): b.pop () for an in b: a = a.replace ('[,') .replace (']' '') temp.append (float (a)) result.append (temp) # print ("temp printed midway is", temp) # print ("result added to result is", result) deletes specific characters in str

Delete the redundant string strip () at the beginning and end of the string

# remove redundant characters from the string def string_remove (): str1 = 'abc' print str1.strip () # abc str2 ='- abcdf++++' print str2.strip ('- +') # abcdf

The replace function, which deletes all strings in a string

Ss = 'old old string'ret = ss.replace (' old', 'new', 1) print (ret)

Sub function, which deletes multiple strings at the same time, where regular expressions are used

Str2 = 'abcwrt22666' # Delete all the strings, import reprint (re.sub ('[]','', str2)) # abcwrt22666 Thank you for your reading. The above is the content of "how to read and write python Files". After the study of this article, I believe you have a deeper understanding of how to read and write python files. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report