python入门之(文件操作和用户定义的类)

#文件操作
>>> file = open("data.txt","w")
>>> file.write('hello python file\n')
18
>>> f.close()

>>> #读取文件内容

>>> file=open('data.txt')
>>> text = file.read()
>>> text
'hello python file\n'
>>> print(text)
hello python file

>>> text.split()
['hello', 'python', 'file']
>>> file.close()
>>> 
>>> dir(file)
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline', 'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines']
>>> help(file.readline)
Help on built-in function readline:

readline(size=-1, /) method of _io.TextIOWrapper instance
    Read until newline or EOF.

    Returns an empty string if EOF is hit immediately.

>>> f = open('data.txt')
>>> text = f.readline()
>>> text
'hello python file\n'
>>> text = f.readline(2)
>>> text
'数据'
>>> text = f.readlines()
>>> text
['挖掘\n', 'Python编程\n', '数学分析']
>>> f.close()
>>> data = open('data.txt')
>>> data = data.read()
>>> data
'hello python file\n数据挖掘\nPython编程\n数学分析'
>>> print(data)
hello python file
数据挖掘
Python编程
数学分析

>>> data = open('data.txt','rb').read()
>>> data
b'hello python file\r\n\xca\xfd\xbe\xdd\xcd\xda\xbe\xf2\r\nPython\xb1\xe0\xb3\xcc\r\n\xca\xfd\xd1\xa7\xb7\xd6\xce\xf6'
>>> 
>>> 
>>> 
>>> #set数据类型
>>> x = set('spam')
>>> x
{'p', 'a', 's', 'm'}
>>> y={'h', 'a', 'm'}
>>> y
{'h', 'a', 'm'}
>>> x ,y
({'p', 'a', 's', 'm'}, {'h', 'a', 'm'})
>>> x&y
{'a', 'm'}
>>> x|y
{'h', 'm', 'p', 'a', 's'}
>>> x-y
{'p', 's'}
>>> 
>>> 
>>> 3<1
False
>>> 3>2
True
>>> x = None
>>> print(x)
None


>>> #用户定义的类

>>> class Me:
    def __init__(self,name,pay):
        self.name = name
        self.pay = pay
    def lastName(self):
        return self.name.split()[-1]
    def giveRaise(self,percent):
        self.pay *=(1.0+percent)


>>> bob = Me('Bob Smith', 5000 )
>>> sue = Me("Sue Jones", 6000)
>>> bob.lastName()
'Smith'
>>> sue.lastName()
'Jones'
>>> sue.giveRaise(.10)
>>> sue.pay
6600.000000000001
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值