>>> 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])
各个参数的解释如下:
(2)file对象的属性
属性 | 描述 |
---|---|
f.name | 返回文件的名称 |
f.mode | 返回被打开文件的访问模式 |
f.closed | 返回true如果文件已被关闭,否则返回false,返回true如果文件已被关闭,否则返回false。 |
不同模式打开文件的完全列表:
模式 | 描述 |
---|---|
r | 以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。 |
rb | 以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。 |
r+ | 打开一个文件用于读写。文件指针将会放在文件的开头。 |
rb+ | 以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。 |
w | 打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
wb | 以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
w+ | 打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
wb+ | 以二进制格式打开一个文件用于读写。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。 |
a | 打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。 |
ab | 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。 |
a+ | 打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。 |
ab+ | 以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。 |
file对象的 close()方法刷新缓冲区里任何还没写入的信息,并关闭该文件,这之后便不能再进行写入。当一个文件对象的引用被重新指定给另一个文件时,Python 会关闭之前的文件。用 close()方法关闭文件是一个很好的习惯。
因为文件对象会占用计算机操作系统的资源,并且操作系统同一时间能打开的文件数量也是有限的,为节省计算机资源和提高性能,我们需要在使用完文件后,将其关闭。
file对象提供了一系列方法,能让我们的文件访问更轻松。来看看如何使用read()和write()方法来读取和写入文件。
write()方法可将任何字符串写入一个打开的文件。需要重点注意的是,Python字符串可以是二进制数据,而不是仅仅是文字。write()方法不会在字符串的结尾添加换行符(’\n’):
语法:
f.write(string)
在这里,被传递的参数是要写入到已打开文件的内容。
如:
>>> f = open("TestFile.txt", "w+", encoding="utf-8")
>>> f.write("青花瓷十分好听!")
8
>>> f.close()
>>>
上述方法会创建TestFile.txt文件,并将收到的内容写入该文件,并最终关闭文件。如果你打开这个文件,将看到以下内容:
青花瓷十分好听!
read() 一次读取全部文件内容。这里,Python字符串可以是二进制数据,而不是仅仅是文字。
语法:
fileObject.read([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()
>>>
f.readline([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 = open(文件名)
f.seek([偏移字节数], 0) # 开头
f.seek([偏移字节数], 1) # 当前操作的位置
f.seek([偏移字节数], 2) # 末尾
>>> 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()