python

1.文件处理

open()

"r,a,w,x"

r:读取

a:追加,读取并创建

w:进行写入

x:创建

 

f = open("1.txt","rt")

如需打开文件,请使用内建的 open() 函数。

open() 函数返回文件对象,此对象有一个 read() 方法用于读取文件的内容:

f = open("demofile.txt", "r")
print(f.read())

返回某一行

f = open("demofile.txt", "r")
print(f.readline())

逐行遍历文件

f = open("demofile.txt", "r")
for x in f:
  print(x)
x是一个变量;

关闭文件

 

f = open("demofile.txt", "r")
print(f.readline())
f.close()

字符串
a = "Hello"
print(a)
多行字符串
a = '''Python is a widely used general-purpose, high level programming language. 
It was initially designed by Guido van Rossum in 1991 
and developed by Python Software Foundation. 
It was mainly developed for emphasis on code readability, 
and its syntax allows programmers to express concepts in fewer lines of code.'''
print(a)

函数
调用
def my_function():
  print("Hello from a function")

my_function()

参数(函数中会用到的x)
def my_function(fname):
  print(fname + " Gates")

my_function("Bill")
my_function("Steve")
my_function("Elon")

以list传参
def my_function(food):
  for x in food:
    print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)

列表
thislist = ["apple", "banana", "cherry"]
print(thislist)
访问print(thislist【1】)
负的索引

索引范围
更改值
遍历列表

py的正则表达式
import re
re.match #从开始位置开始匹配,如果开头没有则无

re.search #搜索整个字符串

re.findall #搜索整个字符串,返回一个list


 
 
 
 



 

posted @ 2022-05-17 11:06  rox丶小花生  阅读(25)  评论(0编辑  收藏  举报