[PYTHON] Data handling 1 Data formatting and file input / output

Aidemy 2020/10/11


Hello, it is Yope! I am a liberal arts student, but I was interested in the possibilities of AI, so I went to the AI-specialized school "Aidemy" to study. I would like to share the knowledge gained here with you, and I am summarizing it on Qiita. I am very happy that many people have read the previous summary article. Thank you! This is the first post on data handling. Nice to meet you.

What to learn this time ・ How to transform text data -How to divide the character string ・ How to input / output files

(Partial review) Text data formatting

Convert an object to a string (str)

-Can be converted to a character string type with __str (object) __.

Embed variables in strings

print("I{}is".format(name)) #Iんがょぺis

When a dictionary type is specified in the argument of format ()

-If you specify the dictionary key in {} included in the character string of format (), the key value can be output.

dic={"name":"Charmander", "gen":1}
print("{0[name]}I decided on you!".format(dic)) #ヒトカゲI decided on you!

Specify the character width

-The character width can be specified by specifying the following in {} where format () is entered. ・ Center: __ {: ^ 10} .format () __ ・ Move to the left: __ {: <10} .format () __ ・ Move to the right: __ {:> 10} .format () __

Splitting strings

Split into list

-Split can be done with __.split ("delimiter (symbol)") __.

poke="Bulbasaur, Charmander, Squirtle, Pikachu"
print(poke.split("、")) #["Bulbasaur","Charmander","Squirtle","Pikachu"]

Make the list a single string (the opposite of split)

-__ "Characters (symbols) you want to connect" .join (list) __ can be connected.

print("-".join(list)) #2020-10-4

File input / output

Open file

-When you want to handle files programmatically (for example, data files obtained from the Web) It can be opened with __open (filename, mode) __. -Modes include writable "" w "(overwrite)" "" a "(additional)", readable "" r "", and readable and writable "" r + "".

Close file

-When you open a file, you must close it (because the processing becomes heavy). -It can be closed with __file.close () __.

Write to file

-Can be written with __f.write ("contents") __. -Note that in "w" mode, this "content" will be overwritten.

Read the file

-When the file mode is "r" or "r +", it can be read with __f.read () __. -Use f.readline () when you want to read only one line.

Open and close files automatically

-If you use the with statement like __with open ("filename", "mode") as f: __, the file will be closed automatically. Therefore, in practice, files are usually handled using the with statement.

#read.Read txt with with statement
with open("read.txt","r") as f:
#text.Overwrite txt with with statement
with open("text.txt","w") as f:
    f.write("I'm hanga")


-Characters can be embedded in the {} of the character string with __.format () __. Characters to be embedded can be dictionary type or list type. ・ Characters can be aligned by using : ^ 10 in {}. -You can split a character string with __split () __. -You can open the file with __open () __. You can close the file with __close () __, write it with __.write () __, and read it with __read () __. -When handling a file, it is convenient to do it in with statement because it is not necessary to close it.

This time is over. Thank you for reading until the end.

Recommended Posts

Data handling 1 Data formatting and file input / output
Python application: Data handling Part 1: Data formatting and file input / output
python input and output
Tips on Python file input / output
Notes for Python file input / output
Data handling
Data input / output in Python (CSV, JSON)
Output python log to both console and file
Multi-condition data handling
Sort Fashion-MNIST data and save as PNG file
Python-Read data from a numeric data file and calculate covariance
Story of image analysis of PDF file and data extraction
Read json file with Python, format it, and output json
Output a binary dump in binary and revert to a binary file
Python audio input / output
Standard input / output summary