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

眼科医师

读取每个文件后,我想写一个新行。从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()
ArunJose_Intel

采用

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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何写文件但保持\ n而不是在python中添加新行?

来自分类Dev

循环读取:如何写图像名称?

来自分类Dev

如何写htaccess文件,更改URL

来自分类Dev

如何写一些文件?

来自分类Dev

如何写文件的特定行长?

来自分类Dev

使用Excel作为日志读取器。如果Excel打开,如何写日志文件?

来自分类Dev

使用Excel作为日志读取器。如果Excel打开,如何写日志文件?

来自分类Dev

如何写没有最后一行换行符的csv文件?

来自分类Dev

如何写元素

来自分类Dev

乳胶中s后如何写撇号

来自分类Dev

如何写入/读取已锁定的文件?

来自分类Dev

vim如何写一个只读文件?

来自分类Dev

如何写与uwsgi命令等效的uwsgi ini文件

来自分类Dev

如何写一个“ * .bat”文件?

来自分类Dev

vim如何写一个只读文件?

来自分类Dev

如何写多个if条件

来自分类Dev

如何写SpannableString包裹?

来自分类Dev

如何写根元素?

来自分类Dev

如何写JUEL表达

来自分类Dev

如何写根元素?

来自分类Dev

如何写equals方法

来自分类Dev

如何写单行块

来自分类Dev

如何写入文件的特定行?

来自分类Dev

CSV文件为每个字母写一个新行

来自分类Dev

如何写入/读取“设置”文本文件

来自分类Dev

如何在每个循环后添加新行?

来自分类Dev

如何逐行读取多个文本文件并在每个文件发送到新列后发送到excel?

来自分类Dev

phpStorm10-如何写“ [”括号

来自分类Dev

if / else if / else如何写速记?