读取每个文件后如何写一个新行?

how do i write a new row after reading each file?

我想在读取每个文件后写一个新行。这是从 KML 到 CSV。

当天: - 每个文件都应该将值写入列 - 每个 kml 文件的新行

from bs4 import BeautifulSoup
import csv
import os
import glob

def process_coordinate_string(str):

    comma_split = str.split(',')
    return [comma_split[1].strip(), comma_split[0].strip()]


def main():
    """
    Open the KML. Read the KML. Open a CSV file. Process a coordinate string to be a CSV row.
    """

    files = []
    for i in os.listdir('<dir>'):
        if i.endswith('.kml'):
            with open(i, 'r') as f:
                s = BeautifulSoup(f, 'xml')
                with open('out.csv', 'w') as csvfile:
                    writer = csv.writer(csvfile,lineterminator='')
                    for names in s.find_all('name',):
                        writer.writerow(names)

                    for coords in s.find_all('coordinates'):
                        writer.writerow(process_coordinate_string(coords.string))



main()

使用

with open('out.csv', 'a') as csvfile:

而不是

with open('out.csv', 'w') as csvfile:



from bs4 import BeautifulSoup
import csv
import os
import glob

def process_coordinate_string(str):

    comma_split = str.split(',')
    return [comma_split[1].strip(), comma_split[0].strip()]


def main():
    """
    Open the KML. Read the KML. Open a CSV file. Process a coordinate string to be a CSV row.
    """

    files = []
    for i in os.listdir('.'):
        if i.endswith('.kml'):
            with open(i, 'r') as f:
                s = BeautifulSoup(f, 'xml')
                with open('out.csv', 'a',) as csvfile:
                    writer = csv.writer(csvfile,lineterminator='')
                    for names in s.find_all('name',):
                        writer.writerow(names)

                    for coords in s.find_all('coordinates'):
                        writer.writerow(process_coordinate_string(coords.string))

                        print(dir(writer))
                    writer.writerow("\n")



main()