-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_io_example.py
113 lines (97 loc) · 3.16 KB
/
file_io_example.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
105
106
107
108
109
110
111
112
113
# -*- coding: utf8 -*-
def get_file_contents(filename):
f=open(filename,"r",encoding="utf8")
contents=f.read()
f.close()
# '''
# Input:
# - filename : 문자열값으로 처리할 파일의 이름
# Output:
# - 파일에 나오는 모든 Text 데이터를 문자열로 반환
# Examples:
# >>> import file_io_example as fie
# >>> fie.get_file_contents("1984.txt").split("\n")[0]
# GEORGE ORWELL
return contents
def get_number_of_characters_with_blank(filename):
contents=get_file_contents(filename)
result=len(contents)
return result
# '''
# Input:
# - filename : 문자열값으로 처리할 파일의 이름
# Output:
# - 빈칸을 포함하여 해당 파일에 나오는 글자 수의 총합
# Examples:
# >>> import file_io_example as fie
# >>> fie.get_number_of_characters_with_blank("1984.txt")
# 558841
# ===Modify codes below=============
# ==================================
def get_number_of_characters_without_blank(filename):
# '''
# Input:
# - filename : 문자열값으로 처리할 파일의 이름
# Output:
# - 빈칸을 포함하지 않는 해당 파일에 나오는 글자 수의 총합
# 여기서 빈칸이라고 함은 " ", "\t", "\n" 을 모두 포함함
# Examples:
# >>> import file_io_example as fie
# >>> fie.get_number_of_characters_without_blank("1984.txt")
# 459038
# ===Modify codes below=============
contents=get_file_contents(filename)
en=0
tn=0
nn=0
nlen=len(contents)
for i in contents:
if " " in i:
en+=1
elif "\t" in i:
tn+=1
elif "\n" in i:
nn+=1
result= nlen-(en+tn+nn)
# ==================================
return result
def get_number_of_lines(filename):
# '''
# Input:
# - filename : 문자열값으로 처리할 파일의 이름
# Output:
# - 해당 파일의 모든 라인수의 총합
# 단 마지막 줄 수는 제외함
# Examples:
# >>> import file_io_example as fie
# >>> fie.get_number_of_lines("1984.txt")
# 1414
# ===Modify codes below=============
contents_list=get_file_contents(filename).strip()
nc=contents_list.split("\n")
result=len(nc)
# ==================================
return result
def get_number_of_target_words(filename, target_words):
# '''
# Input:
# - filename : 문자열값으로 처리할 파일의 이름
# - target_words : 문자열값으로 처리할 파일의 이름
# Output:
# - 대소문자 구분없이 해당 파일에 target_words가 포함된 횟수
# Examples:
# >>> import file_io_example as fie
# >>> fie.get_number_of_words("1984.txt", "Hi")
# 3938
# >>> fie.get_number_of_words("1984.txt", "had")
# 1327
# >>> fie.get_number_of_words("1984.txt", "and")
# 2750
# ===Modify codes below=============
count=0
content=get_file_contents(filename)
newcontent=content.lower()
newtarget_words=target_words.lower()
count=(newcontent.count(newtarget_words))
# ==================================
return count