Python

>>> time.localtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
>>>
>>> time.gmtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
>>>

结构化时间和时间戳的转化

time.mktime(结构化时间)

demo:

time.mktime(time.localtime())

字符串时间和结构化时间的转化

time.strptime(时间字符串,字符串对应格式)

demo:

print(time.strptime("2019-01-17","%Y-%m-%d"))
#time.struct_time(tm_year=2019, tm_mon=1, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=17, tm_isdst=-1)

print(time.strptime("04/17/2019","%m/%d/%Y"))
#time.struct_time(tm_year=2019, tm_mon=4, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=107, tm_isdst=-1)

文件:

文件概念:

编程中,很多时候都离不开对文件的操作。在人工智能中,很多东西会涉及到数据科学,经常需要我们处理一些数据。这些数据通常会保存在一些文件里,然后,我们运用Python以及相关的数据科学等库生成或者处理这些文件。

在Python中,常用的可以生成或者读取的文件类型有很多,比如txt, csv, xlsx, json, xml,html, mp3, mp4等文件。

文件的打开 与关闭

(1)open()

用Python进行文件的读取操作,必须先用Python内置的open()函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写

语法格式如下:

f=open(file_[]name[,mode][,encoding="utf-8"][,buffering])

各个参数的解释如下:

  • file_object是open()返回的文件对象;
  • file_name:是要访问的—文件名的字符串值。
  • mode:决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表,这个参数是非强制的,默认文件访问模式为只读®。
  • encoding指定文件编码,一般使用"utf-8"或者"gbk"。
  • buffering:如果buffering的值被设为0,就不会有寄存;如果buffering的值取1,访问文件时会寄存行;如果将buffering的值设为大于1的整数,表明了这就是的寄存区的缓冲大小;如果取负值,寄存区的缓冲大小则为系统默认。

(2)file对象的属性

属性描述
f.name返回文件的名称
f.mode返回被打开文件的访问模式
f.closed返回true如果文件已被关闭,否则返回false,返回true如果文件已被关闭,否则返回false。

不同模式打开文件的完全列表:

模式描述
r以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。
r+打开一个文件用于读写。文件指针将会放在文件的开头。
rb+以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。
w打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
w+打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
wb+以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
a打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

(3) close() 函数

    file对象的 close()方法刷新缓冲区里任何还没写入的信息,并关闭该文件,这之后便不能再进行写入。当一个文件对象的引用被重新指定给另一个文件时,Python 会关闭之前的文件。用 close()方法关闭文件是一个很好的习惯。

因为文件对象会占用计算机操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的,为节省计算机资源和提高性能,我们需要在使用完文件后,将其关闭。

文件的读写操作

​ file对象提供了一系列方法,能让我们的文件访问更轻松。来看看如何使用read()和write()方法来读取和写入文件。

(1) write()

​ write()方法可将任何字符串写入一个打开的文件。需要重点注意的是,Python字符串可以是二进制数据,而不是仅仅是文字。write()方法不会在字符串的结尾添加换行符(’\n’):

语法:

f.write(string)

在这里,被传递的参数是要写入到已打开文件的内容。

如:

>>> f = open("TestFile.txt", "w+", encoding="utf-8")
>>> f.write("青花瓷十分好听!")
8
>>> f.close()
>>>

上述方法会创建TestFile.txt文件,并将收到的内容写入该文件,并最终关闭文件。如果你打开这个文件,将看到以下内容:

青花瓷十分好听!

(2) read()方法

read() 一次读取全部文件内容。这里,Python字符串可以是二进制数据,而不是仅仅是文字。

语法:

fileObject.read([count])
  • 该方法从文件的开头开始读入,传递的count是要从已打开文件中读取的字节计数。
  • 如果没有传入count,读取全部文件内容。

例:这里我们用到以上创建的 TestFile.txt 文件。

>>> f = open("GreenFlowerPorcelain.txt", "r")
>>> sr1 = f.read(20)
>>> sr1
' 1 Green Flower Porc'
>>> len(sr1)
20
>>> f = open("GreenFlowerPorcelain.txt", "r")
>>> f.read()
" 1 Green Flower Porcelain\n 2 You are the face that has changed my whole world.\n 3 You are the face that I see everywhere I go.\n 4 You are so beautiful to me that I can't explain ,\n 5 Just like a green flower porce lain.\n 6 You're like a moon that I awaken to say hello,\n 7 So beautiful and bright that you make me content to play it so.\n 8 I see your face on the leaves,telling me how lonely I have been.\n 9 This is a dream of mine that I have just dreamed.\n10 Just see your smiling face everywhere I go.\n11 The love I feel for you to shine inside me.\n12 But it's all over now you're gone."
>>>

(3)readline()方法

  • f.readline()会从文件中读取单独的一行。换行符为‘\n’

  • f.readline()如果返回一个空字符串,说明已经读取到最后一行。

    >>> f=open("GreenFlowerPorcelain.txt","r")
    >>> f.readline()
    ' 1 Green Flower Porcelain\n'
    >>> f.readline()
    ' 2 You are the face that has changed my whole world.\n'
    >>> f.readline()
    ' 3 You are the face that I see everywhere I go.\n'
    >>> f.readline()
    " 4 You are so beautiful to me that I can't explain ,\n"
    >>> for i in range(4):
    ...     f.readline()
    ...' 5 Just like a green flower porcelain.\n'
    " 6 You're like a moon that I awaken to say hello,\n"
    ' 7 So beautiful and bright that you make me content to play it so.\n'
    ' 8 I see your face on the leaves,telling me how lonely I have been.\n'
    >>>
    >>> f.readline(2)
    ' 9'
    >>> f.readline(20)
    ' This is a dream of '
    >>>
    >>> f.close()
    >>>
    

(4)readlines()方法

f.readline([sizehint])

  • 以列表的形式返回该文件中包含的所有行,列表中的一项表示文件的一行。
  • 如果设置可选参数sizehint,则读取指定长度的字节,并且将这些字节按行分割
>>> f=open("GreenFlowerPorcelain.txt","r")
>>> f.readlines()
[' 1 Green Flower Porcelain\n', ' 2 You are the face that has changed my whole world.\n', ' 3 You are the face that I see everywhere I go.\n', " 4 You are so beautiful to me that I can't explain ,\n", ' 5 Just like a green flower porcelain.\n', " 6 You're like a moon that I awaken to say hello,\n", ' 7 So beautiful and bright that you make me content to play it so.\n', ' 8 I see your face on the leaves,telling me how lonely I have been.\n', ' 9 This is a dream of mine that I have just dreamed.\n', '10 Just see your smiling face everywhere I go.\n', '11 The love I feel for you to shine inside me.\n', "12 But it's all over now you're gone."]
>>>

另一种方式是迭代一个文件对象然后读取每行

>>> f=open("GreenFlowerPorcelain.txt","r")
>>> for line in f:
...     print(line)
...
 1 Green Flower Porcelain

 2 You are the face that has changed my whole world.

 3 You are the face that I see everywhere I go.

 4 You are so beautiful to me that I can't explain ,

 5 Just like a green flower porcelain.

 6 You're like a moon that I awaken to say hello,

 7 So beautiful and bright that you make me content to play it so.

 8 I see your face on the leaves,telling me how lonely I have been.

 9 This is a dream of mine that I have just dreamed.

10 Just see your smiling face everywhere I go.

11 The love I feel for you to shine inside me.

12 But it's all over now you're gone.
>>>
>>> f=open("GreenFlowerPorcelain.txt","r")
>>> for line in f:
...     print(line,end=" ")
...
 1 Green Flower Porcelain
  2 You are the face that has changed my whole world.
  3 You are the face that I see everywhere I go.
  4 You are so beautiful to me that I can't explain ,
  5 Just like a green flower porcelain.
  6 You're like a moon that I awaken to say hello,
  7 So beautiful and bright that you make me content to play it so.
  8 I see your face on the leaves,telling me how lonely I have been.
  9 This is a dream of mine that I have just dreamed.
 10 Just see your smiling face everywhere I go.
 11 The love I feel for you to shine inside me.
 12 But it's all over now you're gone. >>>
>>> f=open("GreenFlowerPorcelain.txt","r")
>>> for line in f:
...     print(line.strip())
...
1 Green Flower Porcelain
2 You are the face that has changed my whole world.
3 You are the face that I see everywhere I go.
4 You are so beautiful to me that I can't explain ,
5 Just like a green flower porcelain.
6 You're like a moon that I awaken to say hello,
7 So beautiful and bright that you make me content to play it so.
8 I see your face on the leaves,telling me how lonely I have been.
9 This is a dream of mine that I have just dreamed.
10 Just see your smiling face everywhere I go.
11 The love I feel for you to shine inside me.
12 But it's all over now you're gone.
>>>

我们在写程序的时候,读写随时可能产生误会,一旦出错,无法调用close()方法,就不能保证出错时能够正确关闭文件。

所以,为了保证无论是否出错都能正确地关闭文件我们可以用下列方法:

with open("GreenFlowerPorcelain.txt","r",encoding="utf-8")as f:
    print(f.read())
    
    
    
    
with open("GreenFlowerPorcelain.txt","w",encoding="utf-8")as f:
    print(f.read())
    f.write('Hello,World')

文件定位读取

  • f.seek()
    • seek(offset [,from]) 改变当前文件的位置。
      • Offset变量表示要移动的字节数。
      • From变量指定开始移动字节的参考位置。如果from被设为0,这意味着将文件的开头作为移动字节的参考位置。如果设为1,则使用当前的位置作为参考位置。如果它被设为2,那么该文件的末尾将作为参考位置。
f = open(文件名)
f.seek([偏移字节数], 0) # 开头
f.seek([偏移字节数], 1) # 当前操作的位置
f.seek([偏移字节数], 2) # 末尾
  • f.tell()
    • tell() 获取文件内的当前位置;换句话说,下一次的读写会发生在文件开头这么多字节之后。
      • Off
>>> f=open("GreenFlowerPorcelain.txt")
>>> f.read(30)
' 1 Green Flower Porcelain\n 2 Y'
>>> f.tell()
31
>>> f.seek(0,0)
0
>>> f.tell()
0
>>> f.seek(32)
32
>>> f.tell()
32
>>> f.read(30)
'u are the face that has change'
>>>
  • 文件重命名
>>>import os
>>> os.rename("TestFile.txt","TestFile.md")#两个参数,当前和新
>>>
  • 文件删除
>>> import os
>>> os.remove("TestFile.md")

目录操作

  • 文件夹创建
>>> import os
>>>os.mkdir("test_folder")
  • 获取当前目录
>>> import os
>>>os.getcwd()
  • 删除目录
>>>import os
>>> os.rmdir("test_folder")
  • 改变默认目录(指定文件夹)
>>>import os
>>> os.chdir("../")
>
  • 获取目录列表
>>>import os
>>> os.listdir()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值