简体   繁体   中英

to_csv saves only the last occurence (python)

I have a df resulting from a loop (which is part of a longer program that's why I'm not posting it).

If I use print I get the whole df if I use write I get the whole df if I use to_csv I get only the last occurence (regardless the indentation)

Thank you for the help

Edit: code added

j = range(14)
selections = []
for ind in j:
    selections.append(files[ind])
    for selection in selections:
        first_classes = selection.find('ul', class_='first_class')
        second_classes= first_class.find_all('li', class_='second_class')
        list = []
    for second_class in second_classes:
        name = second_class.find('a').text
        list.append(name)
    i = range(5)
    h = []
    for index in i:
        h.append(list[index])
    df = pd.DataFrame(h)
    df.to_csv('myfile.csv', index=False, header=False)

您需要在它之前的循环中获取最后一个for循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM