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

Python learning ---day7

編輯:Python

Data analysis

First step : Collect data ( Data sources : Reptiles ( Network data sources )、 Questionnaire collection 、 Buy data from specialized companies 、 The database established by the company )
The second step : Data preprocessing ( Tools :python、excel、 Use both hands )
The third step : With the aid of algorithms 、 Model, etc
cost reduction , Increase revenue

File operations

Data persistence : Data changes from transient state to persistent state
open( Parameters 1、 Parameters 2、 Parameters 3…) — Open file , Open a file object

  • Parameters 1: Path and filename

  • Parameters 2: The way the file is opened ( Read and write )

  • Parameters 3: The encoding form of file opening :utf-8

Parameters 1

  • grammar : '.‘ Represents the current folder ( You can omit it );’…' To return to the next level

  • Between folders and between folders and files ’/' interval
    Path can be divided into relative path and absolute path

  • Relative paths : Based on the current reference , Another file corresponds to the location of the reference , You should use ’/’

  • Absolute path : Take the disk as a reference , Use ’’
    Suppose you are currently in a folder B in , If you want to find a file A: …/ Folder A/ file A

open('./test/ file 1')

Parameters 2:

r a w +

  • r: read-only , If the file doesn't exist 、 Report errors

  • w: Just write , file does not exist , First automatically create ; If the file exists , Empty first ( If a folder does not exist , Don't create a folder )

  • a: Just write , file does not exist , Report errors ; If the file exists , Additional

  • +: Can read but write , take r、a、w Become readable and writable symbols :r+、a+、w+;python Operation file , Read or write , You cannot read and write at the same time
    r、w、a、+ —> File editing method
    t、b ----> Document presentation form
    t: Text
    b: byte
    r、w、a Equivalent to rt、wt、at -rb、wb、ab、br、bw、ba Represents the operation of a file in bytes

Parameters 3

encoding = ‘utf-8’

f = open(r'./test/ file 1', 'r', encoding='utf-8')print(f)

read() Read information from file objects

result = f.read()print(result, type(result))

notes : All files are stored in binary in memory

f1 = open(r'./test/123.jpg', 'rb')result1 = f1.read()print(result1, type(result1))

File write operation

  • newly build xxx Name file

  • Write content

  • Keep close

  • Write operations

f = open('./test/ Geese .txt', 'w', encoding='utf-8')str_1 = '\t Chant goose \n\t lo bingwang \n Goose, goose and goose , Song item to the sky .\n White hair dew , Red palm dials clear waves .'print(str_1)f.write(str_1)f.write('\n Chant goose ')f.close()# f.write('1')# ValueError: I/O operation on closed file.# I/O--Input/Out

File read operation

f = open('./test/ Geese .txt', 'r', encoding='utf-8')

read(): Read everything at once , The read result is a string

print(f.read())

readline(): Read one line at a time , The read result is a string

print(f.readline())

readlines(): Read all at once , Each line is treated as an element in the list

print(f.readlines())f.close()

notes
seek()— The pointer : During file operation, the pointer is at the beginning of the file by default , The pointer changes as the file is manipulated .

Picture reading and writing operation

Read the picture

f1 = open('./test/ Administrators .jpeg', 'rb')result = f1.read()print(result)f1.close()

Download the pictures

f2 = open('./test/photo1.jpeg', 'wb')f2.write(result)f2.close()

author : Have a strong sense of self-management .

Game programming , A game development favorite ~

If the picture is not displayed for a long time , Please use Chrome Kernel browser .


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