Python application: Data handling Part 1: Data formatting and file input / output

A little review content will be included

Data formatting and file input / output

Convert an object to a string

It is a method to change the shape as a simple error.

#String conversion
year = str(year)

#By the way, number conversion
year = int(year)

Embed characters in format

You can put characters in {} in the text with .format.

#Substitute the number 20 for the variable age
age = 20

#In the variable msg,"I,{}I'm old."Substitute the string
msg = "I,{}I'm old."

#Using the variables msg and age"I am 20 years old."Output
print(msg.format(age))

#Output result
I am 20 years old.

Embed characters in multiple formats

With format (), in the order of the arguments Sequentially assigned in {}.

print("I,{}From{}I went on a trip.".format("Tokyo", "Osaka"))
#Output result
I went on a trip from Tokyo to Osaka.

Embed characters in specified order

For format, if you put a number in {}, in that order Will be substituted.

For list type, specify the index number with [].


print("my name is,{0} {1}is.".format("Ishikawa", "Akihiko"))
#Output result
"My name is Ishikawa Akihiko."
print("my name is,{1} {0}is.".format("Ishikawa", "Akihiko"))
#Output result
"My name is Akihiko Ishikawa."

#Str the list type object.format()When taking as an argument of a function
#Specify as follows. Square brackets[]Numbers with are indicating the order in the list
#The number without it indicates the number of the argument of the format function.

list = ["Ishikawa", "Akihiko"]
"my name is,{0[0]} {0[1]}",format(list)
=> "My name is Ishikawa Akihiko."

In the case of dictionary type, the value is the second argument. In other words It will be in the form of {1 []}.

dic = {
    "Night view" : ["Kobe", "Hakodate", "Nagasaki"],
    "Festival" : ["祇園Festival", "天神Festival", "神田Festival"],
    "Famous waterfall" : ["Kegon Falls", "Nachi Falls", "Fukuroda Falls"]
}
template = "In general, Japan's three major{0}Is "{1[0]}」、「{1[1]}」、「{1[2]}Refers to."

#Output the contents of the variable dic in a format like (example).
for key in dic:
    print(template.format(key,dic[key]))

#Output result
Generally, the three major night views of Japan are "Kobe", "Hakodate", and "Nagasaki".
Generally, the three major festivals in Japan are "Gion Matsuri", "Tenjin Matsuri", and "Kanda Matsuri".
In general, the three major waterfalls in Japan are "Kegon Falls," "Nachi Falls," and "Fukuroda Falls."

Embed characters by name

Next, as with the dictionary type handling, it is a method to specify the key and retrieve it.

dic = {
    "CEO" : {"Ai" : "Ishikawa Akihiko", "Apple" : "Tim Cook", "Facebook" : "Mark Zuckerberg" },
    "location" : {"Ai" : "Japan", "Apple" : "America", "Facebook" : "America"},
    "founded_year" : {"Ai" : 2014, "Apple" : 1976, "Facebook" : 2004}
}

#Output the contents of the variable dic in a format like (example).
for key in dic:
    print("{0} Ai : {1[Ai]} Apple : {1[Apple]} Facebook : {1[Facebook]}".format(key, dic[key]))

#Output result

CEO Ai : Ishikawa Akihiko Apple : Tim Cook Facebook : Mark Zuckerberg
location Ai :Japan Apple:America Facebook:America
founded_year Ai : 2014 Apple : 1976 Facebook : 2004

Center, left, and right text

You can freely change the arrangement of the character strings using the str.format () function. The str.format () function uses a colon: inside the curly braces {} Options can be specified. For example, if you want to specify centering for 20 characters, write {: ^ 20}.

#When centering
"{:^20}".format("abcde")

#When left justified

"{:<20}".format("abcde")

#When right-aligned

"{:>20}".format("abcde")
dic = {
    "CEO" : {"Ai" : "Ishikawa Akihiko", "Apple" : "Tim Cook", "Facebook" : "Mark Zuckerbarg" },
    "location" : {"Ai" : "Japan", "Apple" : "America", "Facebook" : "America"},
    "founded_year" : {"Ai" : 2014, "Apple" : 1976, "Facebook" : 2004}
}

# str.format()Align the character widths by specifying function options.
for key in dic:
    print("{0:^20} Ai : {1[Ai]:^20} Apple : {1[Apple]:^20} Facebook : {1[Facebook]:^20}".format(key, dic[key]))

#Output result
        CEO          Ai :   Ishikawa Akihiko   Apple :       Tim Cook       Facebook :   Mark Zuckerbarg   
      location       Ai :        Japan         Apple :       America        Facebook :       America       
    founded_year     Ai :         2014         Apple :         1976         Facebook :         2004   

Splitting / combining character strings

Split a string into a list

.split ("") Specifies the point to split.

url = "http://blog.ai.net"

#The variable url is delimited by dots (`"."`) As str.split()Split by function and variable split_Substitute it in the url.
split_url=url.split(".")

# split_Please output the url.
print(split_url)

#Split split_Extract the last stored character string from the url and output it.
print(split_url[-1])

#Output result
['http://blog', 'ai', 'net']
net

Combine the elements of the list into a single string

Specify where and the list to join with "". join ().

list = ["2000", "12", "31"]
"-".join(list)
print(list)
#Output result
 '2000-12-31'

Text file input / output

When you want to save the execution result of the program to a file When you want to handle the data acquired from the web programmatically You need to treat the file as an object.

Use the open function to get the file object.

open(file name,mode)

Now you can get the object of the file specified by the file name.

There are various modes such as "w" (overwrite) or "a" (additional).

mode Contents
"w" When you want to write data from a program to a file
"a" When you want to write data from a program to a file
"r" If you want to read data from a file into a program
"r+" If you want to write and read
#Mode too""Don't forget to enclose it in.

# "hello.txt"Open write-only and assign it to the variable file1.
file1=open("hello.txt","w")

# "test.txt"Open read-only and assign it to the variable file2.
file2 = open("test.txt","r")

Close file

When a file object called f is open

f.close() #You can now close the file.

When you open a file with the open function, you should make a habit of closing it with the close function.

Read text from file

Suppose the open function opens a file object called f.

When you want to write characters to a file from a program

f.write("The character string you want to write") #You can write by using this.

When opened in write-only mode ("w"), the characters in the write function overwrite the contents of the file.

Load text into a file

To programmatically read data from a text file that already contains content Use the read function. If a read-only open file object f exists, do the following:

f.read() #Specify like this.
f.readline() #If you want to read only one line, specify as.

Open files more easily with with

You can operate the file more easily by using the with syntax.

#When reading from a file
with open("file name", "r") as f:
  f.read()

#When writing to a file
with open("file name", "w") as f:
  f.write("What you want to write")

If you write using with, the file will be closed automatically even if a problem occurs in the with block. There is no need to use the close function. To manage file resources safely Make sure to use with to open the file.

Recommended Posts

Python application: Data handling Part 1: Data formatting and file input / output
Python Application: Data Handling Part 3: Data Format
python input and output
Python Application: Data Handling Part 2: Parsing Various Data Formats
Tips on Python file input / output
Notes for Python file input / output
Python application: data visualization part 1: basic
Data input / output in Python (CSV, JSON)
Python Application: Data Visualization Part 3: Various Graphs
Output python log to both console and file
Python audio input / output
Read json file with Python, format it, and output json
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Python application: Pandas Part 1: Basic
Python application: Pandas Part 2: Series
Python application: data visualization # 2: matplotlib
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
perl objects and python class part 2.
Authentication using tweepy-User authentication and application authentication (Python)
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
Python class definitions and instance handling
Python CSV file reading and writing
Hashing data in R and Python
Python application: Numpy Part 3: Double array
Function synthesis and application in Python
Python memo ① Folder and file operations
Data formatting for Python / color plots
perl objects and python class part 1.
Export and output files in Python
[Automation with python! ] Part 2: File operation
Python application: Data cleansing # 2: Data cleansing with DataFrame
CSV output of pulse data with Raspberry Pi (confirm analog input with python)
Put OpenCV in OS X with Homebrew and input / output video with python
Python: Preprocessing in machine learning: Handling of missing, outlier, and imbalanced data
Python Paiza-Various skill checks and standard input
Data pipeline construction with Python and Luigi
AM modulation and demodulation in Python Part 2
FM modulation and demodulation with Python Part 3
Process Pubmed .xml data with python [Part 2]
Python data structure and internal implementation ~ List ~
Copy file and rewrite cell value @python
[Introduction to Udemy Python3 + Application] 41. Input function
[Introduction to Udemy Python3 + Application] 65. Exception handling
Python data structure and operation (Python learning memo ③)
[Python] Web application from 0! Hands-on (4) -Data molding-
Easily graph data in shell and Python
See file and folder information on python
FM modulation and demodulation with Python Part 2
[python] Difference between rand and randn output
Compress python data and write to sqlite
Module import and exception handling in python
[python] Read html file and practice scraping
[Python] Chapter 02-03 Basics of Python programs (input / output)
Exchange encrypted data between Python and C #
[Introduction to cx_Oracle] (Part 9) DB and Python data type mapping (version 8 or later)
Data acquisition from analytics API with Google API Client for python Part 2 Web application
Beautiful graph drawing with python -seaborn makes data analysis and visualization easier Part 1