Python text reading for multiple lines and one line

background

When I studied Python with YOLOv3 code, I wanted to record it as a memo. We will update each content as soon as it is available.

Read text

This time, we will deal with the case where the text content is multiple lines and the case where it is one line.

Sample text: multiple lines

coco_classes.txt


person
bicycle
car
motorbike
aeroplane

Sample code: multiple lines

readlines_.py


import os
classes_path = 'model_data/coco_classes.txt'
#Change to home directory
classes_path = os.path.expanduser(classes_path)
with open(classes_path) as f:
#Make text into one list as character string + line feed
    class_names = f.readlines()

#Put the value on the nth line in the text in c and remove the line breaks
for c in class_names:
    c = c.strip()
    print(c)

Sample text: one line

tiny_yolo_anchors.txt


10,14,  23,27,  37,58,  81,82,  135,169,  344,319

Sample code: one line

readline_.py


import os
anchors_path= 'model_data/tiny_yolo_anchors.txt'
#Change to home directory
anchors_path= os.path.expanduser(classes_path)
with open(anchors_path) as f:
#Make text into one list as character string + line feed
    anchors_path= f.readline()

#In the text','Put the nth value of the value separated by with in x and change it to float type
for x in anchors.split(','):
    x = float(x)
    print(x)

#Accompanying the above process
anchors = [float(x) for x in anchors.split(',')]

I also played with strings

play_with_anchors_value.py


import os
import numpy as np
anchors_path= 'model_data/tiny_yolo_anchors.txt'
with open(anchors_path) as f:
    anchors_path= f.readline()

print(anchors_path)
#10,14,  23,27,  37,58,  81,82,  135,169,  344,319

anchors = [float(x) for x in anchors.split(',')]
print(anchors_path)
#[10.0, 14.0, 23.0, 27.0, 37.0, 58.0, 81.0, 82.0, 135.0, 169.0, 344.0, 319.0]

anchors = np.array(anchors).reshape(-1, 2)#Arrange into two columns of coordinates,-1 is treated as correction
print(anchors_path)
"""
[[ 10.  14.]
 [ 23.  27.]
 [ 37.  58.]
 [ 81.  82.]
 [135. 169.]
 [344. 319.]]
"""

Reflection

Decoding code is (maybe) studying.

Recommended Posts

Python text reading for multiple lines and one line
Reading and writing text in Python
Line graphs and scale lines in python
[Introduction for beginners] Reading and writing Python CSV files
Fizzbuzz in Python (in one line)
[Python] [3D line graph] Multiple data in one graph, axis values in characters
Make python segfault in one line
Build and test a CI environment for multiple versions of Python
uproot: Python / Numpy based library for reading and writing ROOT files
[Python beginner] Divide one list (5 lines).
Python CSV file reading and writing
Reading and writing NetCDF with Python
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)
Reading and creating a mark sheet using Python OpenCV (Tips for reading well)
Read the file with python and delete the line breaks [Notes on reading the file]
Handle multiple python versions in one jupyter
CGI server (1) python edition in one line
6 Python libraries for faster development and debugging
Reading and writing JSON files with Python
(Python) HTML reading and regular expression notes
Process multiple lists with for in Python
[python] Get Twitter timeline for multiple users
Tips for plotting multiple lines with pandas
Decompose command arguments in one line in Python
[Python] Combine multiple Excel sheets into one
[Python] Invert bool value in one line
Add lines and text on the image
Combine multiple python files into one python file
SublimeText2 and SublimeLinter --Syntax check for Python3--
Python --Read data from a numeric data file and find the multiple regression line.
Character code for reading and writing csv files with python ~ windows environment ver ~
python> How to write multiple lines> How to write in mnist_with_summaries.py> It seems that there is no need for line concatenation symbols
Instant method grammar for Python and Ruby (studying)
Causal reasoning and causal search with Python (for beginners)
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Study from Python Reading and writing Hour9 files
[Python & Unix] Combine multiple PDF files into one.
Create a LINE BOT with Minette for Python
Python hand play (one line notation of if)
Reading and writing CSV and JSON files in Python
Make a rock-paper-scissors game in one line (python)
Developed and verified with multiple python versions with direnv
[Python for Hikari] Chapter 09-02 Classes (Creating and instantiating classes)
Format one line of json for easy viewing
[Python / Chrome] Basic settings and operations for scraping
Notify error and execution completion by LINE [Python]
Call Python library for text normalization from MATLAB
Multiple graphs are displayed in one window (python)
PDF files and sites useful for learning Python 3
Reading and writing fits files with Python (memo)
Install Python and libraries for Python on MacOS Catalina
Example of reading and writing CSV with Python
Combine multiple csv files into one csv file with python (assuming only one line of header)
I want to write a triple loop and conditional branch in one line in python