[Solved] How to save data from python into a csv file


the way your code is structured, you iterate over the matches in your for loop, then, when the loop is finished, you save the last match in your CSV. You probably want to write each match in your CSV instead, inside the for loop.

try to replace the last lines of your code (starting at the last for loop) by:

with open('score.csv', 'wt') as f:
    writer = csv.writer(f)
    for dir_path, dir_name_list, file_name_list in os.walk(htmls_path):
        for file_name in file_name_list:
            if not file_name.endswith('.html'):
                continue
            with open(file_name) as markup:
                soup = BeautifulSoup(markup.read())
                text = soup.get_text()
                match = re.findall("PA/(\S*)\s*(\S*)", text)
                print(match)
                writer.writerow(match)

5

solved How to save data from python into a csv file