In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how to read a text file by Python". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to read a text file by Python" can help you solve your doubts.
Look at the code ~ # example.md1 2 34 567 8 9 > with open ('example.md') as f: lines = f.readlines () > lines [' 1 23,'4 56,'7 8 9'] # We find that there is a carriage return after every line We use the strip () function to eliminate it > lines = [i.strip () for i in lines] ['1 23,'4 56,'7 8 9'] # each element is a string, but what we need to read is text data So you need to convert string to int (or float) > > data = [] > > for line in lines: data.append ([int (I) for i in line.split (')]) [[1,2,3], [4,5,6], [7,8,9]] # finally, you can convert list into ndarray form > data = np.array (data) > dataarray ([[1,2,3], [4,5,6], [7]). eight, 9]]) # write the above steps into a function > def read_file (file): "" read .md or .txt format file: param file: .md or .txt format file: return: data "with open ('example.md') as f: lines = f.readlines () data = [] for line" In lines: data.append ([int (I) for i in line.strip (). Split (')]) return np.array (data) > data = read_file ('example.md') > dataarray ([[1]) 2,3], [4, 5, 6], [7, 8, 9])
Add: various ways for python to obtain md5
Look at the code ~ # use the python library to find the MD5import hashlib# string MD5md5 = hashlib.md5 ('string'). Hexdigest () # ask the file md5file = open ('file') 'rb') md5 = hashlib.md5 (file.read ()) hexdigest () file.close () # python uses the mac/linex terminal command to find md5def get_MD5 (file_path):' 'calculate MD5''' files_md5 = os.popen (' md5% s'% file_path). Read (). Strip () file_md5 = files_md5.replace ('MD5 (% s) ='% file_path '') return file_md5# if it is a windows system, you can probably use a similar method to get the untested # when fetching md5 with a large number of files It is recommended to use the command to get it, which will run much faster.
Add: three feasible ways to read txt files in Python
For the contents of the file in DataTest.txt, try not to leave a blank line at the end of the file, otherwise error will appear sometimes.
1,2,3
4,5,6
7,8,9
The first way: use csv.reader () to read the txt file
Import csvdata = [] with open ('ErigerNotTest.txtfold,' rt') as csvfile: reader = csv.reader (csvfile, delimiter=',') for row in reader: data.append (row) # the output is a list print (data)
Output result:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The second way: use numpy.loadtxt () to read the txt file import numpy as np data= np.loadtxt ('Elux paddle DataTest.txtrecording relegation delimiterator training journal') # the output is the array format print (data) in numpy.
Output result:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
However, adding the following statement can be converted to DataFrame format:
Df = pd.DataFrame (data) df.to_csv () print (df)
Output result:
0 1 2
0 1.0 2.0 3.0
1 4.0 5.0 6.0
2 7.0 8.0 9.0
Output result:
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9
After reading this, the article "how to read Python text files" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.