After studying the basics of Python in the paiza learning environment, I was confused that standard input was not possible after migrating to Atom, and this is a summary when I investigated how to read data from the outside. This is a compilation for beginners, so please let me know if you make any mistakes.
I referred to the following site. ** note.nkmk.me ** -Get, change (move) current directory with Python -Reading and writing (creating / adding) files with Python ** crukky ** -Building a test environment for competitive programming with atom / python
pass.py
import os
path = os.getcwd()
print(path)
** os.getcwd () ** returns the absolute path of the current directory where Python is currently running as a string.
If you do not move the .txt file to the folder where the path obtained by os.getcwd () is connected, the file cannot be read properly and an error will occur.
read.py
path = "file name.txt"
with open(path) as f:
s = f.read()
print(s)
It is output in the form of ["character string"].
read2.py
path = "file name.txt"
with open(path) as f:
l_strip1 = [s.strip() for s in f.readlines()]
print(l_strip1)
It is output in the form of ["1st line", "2nd line", "3rd line", "4th line", ...]. If you want to divide the character string separated by "" (space) into a list and read the whole as a two-dimensional list as shown in paiza's skill check, add ** split ("") **.
read3.py
path = "file name.txt"
with open(path) as f:
l_strip2 = [s.strip().split(" ") for s in f.readlines()]
print(l_strip2)
And it is sufficient. Output in the form of [["1-1", "1-2", "1-3"], ["2-1", "2-2", "2-3"], ...] To.
Install the package "** platformio-ide-terminal " from Atom's "File> Settings> Install". Then, a small "+" mark will appear at the bottom left of the screen. Click here to call Terminal on Atom. Write " python (file name) .py " to the right of " PS C: \ Users \ (user name) \ (current directory name)> **" in Terminal and press Enter to Terminal. The .py file is called above.
that's all.
Recommended Posts