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
-Can be converted to a character string type with __str (object) __.
name="Ngahope"
print("I{}is".format(name)) #Iんがょぺis
-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!
-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 () __
-Split can be done with __.split ("delimiter (symbol)") __.
poke="Bulbasaur, Charmander, Squirtle, Pikachu"
print(poke.split("、")) #["Bulbasaur","Charmander","Squirtle","Pikachu"]
-__ "Characters (symbols) you want to connect" .join (list) __ can be connected.
list=["2020","10","4"]
print("-".join(list)) #2020-10-4
-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 + "".
-When you open a file, you must close it (because the processing becomes heavy). -It can be closed with __file.close () __.
-Can be written with __f.write ("contents") __. -Note that in "w" mode, this "content" will be overwritten.
-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.
-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:
f.read()
#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