“Python Read and Write Lines в файле” Ответ

Python Read File Line по строке

with open("file.txt") as file_in:
    lines = []
    for line in file_in:
        lines.append(line)
Caleb McNevin

Читать файлы и записать в другие файлы python

import sys
import glob
import os.path

list_of_files = glob.glob('/Users/Emily/Topics/*.txt') #500 files

for file_name in list_of_files:
    print(file_name)

    # This needs to be done *inside the loop*
    f= open(file_name, 'r')
    lst = []
    for line in f:
       line.strip()
       line = line.replace("\n" ,'')
       line = line.replace("//" , '')
       lst.append(line)
    f.close()

    f=open(os.path.join('/Users/Emily/UpdatedTopics',
    os.path.basename(file_name)) , 'w')

    for line in lst:
       f.write(line)
    f.close()
Joyous Jellyfish

Python Read and Write Lines в файле

import string

def read_file_content(filename):
    fileContent = open(filename, "r")
    return fileContent

def count_words():
    text = read_file_content("./story.txt")
    dictionary = {}
    for line in text:
        line = line.strip()
        line = line.lower()
        
        line = line.translate(line.maketrans("," "", string.puntuation))
        words = line.split()
        
        for word in words:
            if word in dictionary:
                dictionary[word] +=1
            else:
                dictionary[word] = 1
                
    for key in list(dictionary.keys()):
        print(f"{{\"{key}\": {dictionary[key]}}}, ", end=" ")
count_words
Olawale Arowojolu

Ответы похожие на “Python Read and Write Lines в файле”

Вопросы похожие на “Python Read and Write Lines в файле”

Больше похожих ответов на “Python Read and Write Lines в файле” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования