Get the App
SLTechnology News&Howtos  ›  Database  › 

How to get a fixed format file by python

Shulou Source: shulou.com Published: 2022-05-31 16:07:11 09月19日 Update

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!

Tags: Files contents data databases formats programs tests information configurations differences methods more problems frequency security helplessness for this one line function Prophase Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information vpn OPPO Reno Redmi