-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path028+029文件.py
104 lines (87 loc) · 2.53 KB
/
028+029文件.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# help(open)
f = open('/Users/sunyuting/Desktop/yuc/未命名1.txt')
print(f)
print(f.read())
print(f.tell())
print(f.seek(3, 0)) # 文件指针偏移0位置3个字符
print(list(f))
print(f.readline())
print(list(f))
f.seek(0, 0)
for each_line in f:
print(each_line) # 原文档有换行格式,自动逐行打印
f.seek(0,0)
print(f.read(10))
f = open('/Users/sunyuting/Desktop/yuc/未命名2.txt', 'w')
f.write('我爱fishc')
f.close() # 每次文件写完要关闭,不然仅在内存中
# 对record.txt文件操作(test1)
# f = open('/Users/sunyuting/Desktop/yuc/record.txt')
# boy = []
# girl = []
# count = 1
#
# for each_line in f:
# if each_line[:4] != '====':
# # 字符串分割
# (role, line_spoken) = each_line.split(':', 1)
# if role == '小甲鱼':
# boy.append(line_spoken)
# if role == '小客服':
# girl.append(line_spoken)
# else:
# # 文件保存操作
# file_name_boy = 'boy_' + str(count) + '.txt'
# file_name_girl = 'girl_' + str(count) + '.txt'
#
# boy_file = open(file_name_boy, 'w')
# girl_file = open(file_name_girl, 'w')
#
# boy_file.writelines(boy)
# girl_file.writelines(girl)
# boy_file.close()
# girl_file.close()
# boy = []
# girl = []
# count += 1
# file_name_boy = 'boy_' + str(count) + '.txt'
# file_name_girl = 'girl_' + str(count) + '.txt'
#
# boy_file = open(file_name_boy, 'w')
# girl_file = open(file_name_girl, 'w')
#
# boy_file.writelines(boy)
# girl_file.writelines(girl)
# boy_file.close()
# girl_file.close()
# f.close()
#test2(通过定义两个函数来操作)
def save_file(boy,girl,count):
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy, 'w')
girl_file = open(file_name_girl, 'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
def split_file(file_name):
f = open(file_name)
boy = []
girl = []
count = 1
for each_line in f:
if each_line[:4] != '====':
(role, line_spoken) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
save_file(boy,girl,count)
boy = []
girl = []
count += 1
save_file(boy,girl,count)
f.close()
split_file('/Users/sunyuting/Desktop/yuc/record.txt')