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 code examples with python files

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you examples of how to read and write code with python files. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Foreword:

Not long ago, waveform data was read from a .dat file and anomaly detection was performed through a self-coding network. So specially write it here, let's start with the most basic document reading and writing.

First of all, the data is an array of type int16. Say it's an array, but it's not easy to read. It is well known that data is stored in binary form in computers. Int16 signed integer. 16 bits, 16bit, which is two bytes. It's natural to think of reading two bytes of data (that is, 16bit) and converting it into an int16 integer (that is, a decimal integer). The data becomes manageable.

Xiaobai can also learn the series: read and write code examples with python files! (simple case)

First put a different number of integer data range, it is often used.

Xiaobai can also learn the series: read and write code examples with python files! (simple case)

Here is a small knowledge point, the int type in python3 is dynamic length, is a long integer, the theory supports infinite numbers. Run the built-in sizeof method on an int instance to see the number of bytes consumed. Of these, 0 takes up 24 bytes, while other numbers typically take up 28 bytes.

Reading files: text fil

Xiaobai can also learn the series: read and write code examples with python files! (simple case)

First of all, in python3, the string type (str) contains Unicode characters. This feature allows us to define strings that contain Unicode character in python3 and even use non-ASCII characters in variable identifiers.

So what the above code does is define a file object that indicates that the contents of the file will be read by reading characters.

> > f.read ()

'Worldwide Hello'

The above code reads all the contents of the file at once, reads the contents into memory, and represents them as str objects.

You can also call read (size) to read the contents of size bytes at a time. Or call readline () to read one line at a time. Store the contents of each line in str and return a list of str.

To ensure that the file is closed, you can use the with keyword:

Xiaobai can also learn the series: read and write code examples with python files! (simple case)

To read a non-UTF-8-encoded text file, you need to pass the encoding parameter to the open () function, for example, to read a GBK-encoded file:

Xiaobai can also learn the series: read and write code examples with python files! (simple case)

Binary file:

If you want to read the binary file, open the file in rb mode.

Xiaobai can also learn the series: read and write code examples with python files! (simple case)

Write a file:

Writing a file is the same as reading a file. When you call the open () function, pass in'w'or 'wb' to indicate writing a text file or writing a binary file.

The above is the whole content of this article. I hope it will be helpful to your study.

The above is the editor for you to share how to use python files to read and write code examples, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report