程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Advanced Python Programming - file processing and OS module 2

編輯:Python

Catalog

  • 1.txt file
  • 2.yaml File read data
  • 3.openpyxl operation excel
  • 4.csv file It is called a comma separated value file , It can also be called character separator file


1.txt file

# 1. Specific application of documents 
# web automation Interface automation Read the contents of the file Test data yaml file ,excel File data 
# yaml How to write data in a file Reading data 
# 1.yaml File read data 
# 2.excel File read data 
# 3.txt File read 
# 4.csv file 
f=open('./data/test.txt','r',encoding = 'utf-8')
print(f.read())
f.close()
with open('./data/test.txt','r',encoding = 'utf-8') as f:
print(f.read())
print(f.readline())
print(f.readlines())

2.yaml File read data

# Created a yaml file 
# Grammar format :
# 1. Indent means hierarchy Out-of-service tab Key , Use spaces directly 
# 2.# Notation 
# 3. Strings do not need to be quoted , If a number wants to be a string, you need to manually quote it 
# 4. You can create lists [] - Represents a list 
# 5. You can create dictionaries {key:value}: A dictionary 
# 6.yaml The data in should be blank 
# 1. install yaml
# 2.cmd Input pip install pyyaml testing pip list
# 3.setting Inside installation 
# Read yaml The data in the file 
import yaml
# open yaml
f=open('./data/dict1.yaml','r',encoding='utf-8')
# Read load yaml 5.1 I'll use it later Loader=yaml.FullLoder
data=yaml.load(f,Loader=yaml.FullLoader)
print(data)
f.close()
with open('./data/dict1.yaml','r',encoding='utf-8') as f1:
data = yaml.load(f1,Loader = yaml.FullLoader)
print(data['person']['name'])
print(data['adress'])
# data Is to get all the data autumn waters How does the dictionary take values Get the key Get the value 
# problem : How to get the value of autumn water 
#{'code':200,{'name':admin,'sex':' Woman '},adress:'changsha','token':'xxxxx'}
# Read yaml Data in the list 
# 1. Import yaml
# 2. open yaml file 
# 3. load yaml File data 
# 4. Print yaml The contents of the document 
with open('./data/list_yaml.yaml','r',encoding='utf-8') as f:
data = yaml.load(f,Loader=yaml.FullLoader)
print(data)
# Want to take the value of bamboo , How to take it , How to get values from the list How to get the puppy 
# print(data[1])
print(data[1][1])
# List nested Dictionary 
# [{'id':1,'name':' Xiaocui ','age':18},{'id':2,'name':' floret ','age':19}
with open('./data/listdict_yaml.yaml','r',encoding='utf-8') as f:
data=yaml.load(f,Loader=yaml.FullLoader)
print(data)

3.openpyxl operation excel

# openpyxl Only support xlsx I won't support it xls,xls use xlrd,xlwt
# Read excel data xlrd xlwt xlutils pandas openpyxl
#.1. install pip install openpyxl pip list
# operation excel
# Read excel data 
import openpyxl
# open excel
workbook = openpyxl.load_workbook('./data/bb.xlsx')
print(workbook)
# Take the form Specify the data in the form 
sheet = workbook['Sheet3']
print(sheet)
# Read excel The data in it for
for i in sheet.values:
print(i)
# Get all the form data 
sheet=workbook.sheetnames
print(sheet)
# Get all the form data 
# Take out all the forms first Loop list 
# The values in the form workbook[Sheet1], workbook[Sheet2], workbook[Sheet3]
for i in sheet:
print(i)
for j in workbook[i].values:
print(j)
# Not all the data Just a certain value 
# Specify form Specifies the data for the cell 
sheet = workbook['Sheet3']
# Specifies the data for the cell 
id = sheet['A1'].value
print(id)
# # Specifies the data for the cell Row and column 
id = sheet.cell(row=1,column=1).value
print(id)
# Write data 
# sheet.cell(row=1,column=5).value = ' Expected results '
sheet['E1'].value= ' Expected results 3'
workbook.save('./data/bb.xlsx')

4.csv file It is called a comma separated value file , It can also be called character separator file

# Content , separate 
# How to operate csv file 
# 1. Open file 
# 2. Read It's using csv.reader() Back to a csv.reader The object of , Objects that can be iterated 
# We iterate through the object , Output each line , Each column 
import csv
f=open('./data/data.csv','r',encoding='utf-8')
a=csv.reader(f)
for i in a:
print(i)
with open('./data/data.csv','r',encoding='utf-8') as f:
a = csv.reader(f)
print(a)
# for i in a:
# print(i)
# print(i[1])
# Data in a row Turn into 
result = list(a)
print(result)
print(result[1])
# write in writer
stu1 = [1,' autumn waters ',18,' Woman ']
stu2 = [2,' False bamboo ',108,' demon ']
out = open('./data/data.csv','a',encoding='utf-8',newline='')
write=csv.writer(out)
write.writerow(stu1)
write.writerow(stu2)
data = [(' test 1',' test 2'),
(' test 2',' test 2')
(' test 3',' test 2')
(' test 4',' test 2')
(' test 5',' test 2')
]
f1= open('./data/data.csv','a',encoding='utf-8',newline='')
out=csv.writer(f1)
for i in data:
out.writerow(i)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved