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 get a fixed format file by python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Python how to take a fixed format file, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Environment: these days in the process of using python development procedures, you need to connect to the mysql database, and it involves different servers and different databases, early use test server to do testing, do not want to change the database configuration information frequently in the python program, so think of all the database configuration written into a db.conf file to read, is also based on security considerations. So I wrote a file reader program based on a fixed format. The case is as follows. Content of the test file: title11,line 12 line 23 line 23 line 3title24 line 45 line 56 line 6title37 line 78 line 89line 9

Program file:

Def open_file (filename, mode='r'): "": param filename:: param mode:: return: return file handle "try: the_file = open (filename, mode) except IOError: print" Unable to open the file " Filename sys.exit (0) else: return the_filedef next_line (the_file): "": param the_file:: return: read the contents of a line of files "line = the_file.readline () return linedef next_block (the_file):": param the_file:: return: read a piece of content in the specified format " "" title = next_line (the_file) # cg = next_line (the_file) content = [] for i in range (3): # 3 is the number of lines in the file content.append (next_line (the_file)) return title Contentmy_file = open_file ('1.txtexamples,' r') a = raw_input ("enter you name:") # can be called as a variable in a function Here only for i in range (3): # 3 is used for testing the number of segments tit = next_block (my_file) if a = = tit [0] .strip ('\ n'): print "TITLE IS:", tit [0] for j in range (3): print "content", (jung1), "is:" Tit [1] [j] .strip ('\ n') breakelse: print'no this title' exit (0)

In this way, it is good to get my database configuration information as needed. You only need to put title into the python program to locate the corresponding database connection information.

[test]

User:root

Password:123456

Db_name:study

Host:127.0.0.1

Port:3306

Charset:utf8

[mysql]

User:root

Password:123456

Db_name:wwwsite

Host:127.0.0.1

Port:3306

Charset:utf8

After reading the above, have you mastered the method of how to get a fixed format file by python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Database

Wechat

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

12
Report