将新数据附加到.csv?

时间:2013-07-06 03:07:05

标签: python scrape

大家好!我正在尝试从网站上抓取数据并附加任何尚未复制到.csv文件的条目,但我似乎无法正确写入。

url = 'www.website.com'

def getInfo():
    global oldMaxValue, oldMaxRow, newInfo
    newInfo = 0
    with open("file.csv", "a") as f:
        data = requests.get(url)
        text = data.text

        newRows = [line.split(',') for line in text.split("\n") if line]
        newMaxValue = max(row[0] for row in newRows)

        for i in newRows:
            if int(i[0]) > int(oldMaxValue):
                f.write(str(i))

        oldMaxValue = newMaxValue

非常感谢任何指导。非常感谢! :)

1 个答案:

答案 0 :(得分:1)

使用csv模块。这是将单行附加到现有文件的示例:

import csv

f = file("file.csv", 'a')
csv_writer = csv.writer(f)

my_record = ["This", "is", "a", "row", "of", "data"]

csv_writer.writerow(my_record)