def to_space(filename, filename_txt): df = count_lines.read_file(filename) city = list(df["city"]) town = list(df["town"]) newtext = open(filename_txt, "w", encoding="utf-8") # 文字と文字の間をspaceにして新しいテキストに記載 for i in range(len(df)): newtext.write(str(city[i]) + " " + str(town[i]) + "\n") newtext.close() return newtext
def extract_fromm_ton(file, newfilename, mlines, nlines): df = count_lines.read_file(filename) city = list(df["city"]) town = list(df["town"]) newtext = open(newfilename, "w", encoding="utf-8") for i in range(mlines, nlines): newtext.write(str(city[i])+" "+str(i)+"\n") print(str(city[i])+" "+str(town[i])+"\n") newtext.close() return newtext
def extract_first_column(filename, newfilename): df = count_lines.read_file(filename) city = list(df["city"]) city_list = list(set(city)) # 重複を除く newtext = open(newfilename, "w", encoding="utf-8") for i in range(len(city_list)): newtext.write(str(city_list[i]) + "\n") print(str(city_list[i]) + "\n") newtext.close() return newtext
def extract_lines(filename, fromnline, newfilename): df = count_lines.read_file(filename) city = list(df["city"]) town = list(df["town"]) newtext = open(newfilename, "w", encoding="utf-8") # n番目までのデータを表示して新しいテキストに書き込む for i in range(fromnline): newtext.write(str(city[i]) + " " + str(town[i]) + "\n") print(str(city[i]) + " " + str(town[i]) + "\n") newtext.close() return newtext
def frequency(filename, newfilename): df = count_lines.read_file(filename) vc = df["town"].value_counts() total = len(vc) text = open(newfilename, "w", encoding="utf-8") for i in range(total): frequency = vc[i] / total print(vc.index[i] + " :frequency is" + str(frequency)) text.write(vc.index[i] + ": frequency is " + str(frequency) + "\n") text.close() return text
def extract_lastnlines(filename, toline, newfilename): df = count_lines.read_file(filename) city = list(df["city"]) town = list(df["town"]) newtext = open(newfilename, "w", encoding="utf-8") # countでは最後から数えるのでマイナスを指定し、rangeは0からの範囲なので-1して後ろから数えるように指定 for i in range(nlines): count = -(i + 1) newtext.write(str(city[count]) + " " + str(town[count]) + "\n") print(str(city[count]) + " " + str(town[count]) + "\n") newtext.close() return newtext
def extract_first_list(filename, newfilename): df = count_lines.read_file(filename) cities = df["city"].unique() with open(newfilename, mode="w") as f: f.write('\n'.join(cities)) return cities
def extract_lines(filename, fromline, toline): df = count_lines.read_file(filename) print(df[fromline:toline]) extractlines = df[fromline:toline] return extractlines
def count_cities(filename): df = count_lines.read_file(filename) cities = list(df["city"]) dict_cities = count_string(cities) return dict_cities
def count_string(filename): df = count_lines.read_file(filename) vc = df["city"].value_counts() return vc
def extract_lines(filename, fromnline): df = count_lines.read_file(filename) nrows = df.tail(fromnline) return nrows