简体   繁体   English

如何在python中将新行附加到csv文件

[英]How to append a new line to a csv file in python

with open('Price.csv', 'w', newline = '', encoding= 'utf-8') as csvFile:
    csvWriter = csv.writer(csvFile, delimiter=' ')
    csvWriter.writerow("Price")

    for item in items:


        whole_price = item.find_elements(By.XPATH, './/span[@class="a-price-whole"]')
        fraction_price = item.find_elements(By.XPATH, './/span[@class="a-price-fraction"]')

        if whole_price != [] and fraction_price != []:

            price = '.'.join([whole_price[0].text, fraction_price[0].text])
            product_price.append(price)


        else:
            price = 0
   
    csvWriter.writerow(product_price)

driver.quit()

Trying to figure out how to append price to product_price with a new line character at the end.试图弄清楚如何将 price 附加到 product_price 并在末尾添加一个换行符。

This has been my outcome and I'm confused why.这是我的结果,我很困惑为什么。 Do I need to individual print the rows and add a new line.我是否需要单独打印行并添加新行。 I thought writerow added a new line break already?我以为 writerow 已经添加了一个新的换行符?

P r i c e
41.18 48.56 18.73 48.56 48.56 37.46 37.46 53.22 60.99 32.99 18.73 7.79 32.34 39.99 20.49 7.79 34.90 37.25 56.49 48.56 156.00 42.95 85.00 34.98 60.00 17.98 60.61 95.50 6.59 7.49 87.40 74.00 17.73 52.56 34.99 39.99 170.00 18.73 2.

writerow without s at the end is for writing all values on list in one row.最后没有swriterow用于将列表中的所有值写入一行。

And you have all values on one list - so it treats it as single row.而且您在一个列表中拥有所有值 - 因此它将其视为单行。

You should rather write row directly when you find price - but price has to be as list.当您找到价格时,您应该直接写行 - 但price必须作为列表。

            price = '.'.join([whole_price[0].text, fraction_price[0].text])
            
            csvWriter.writerow( [price] )

OR you should append price as list with single value或者您应该将price附加为具有单个值的列表

            price = '.'.join([whole_price[0].text, fraction_price[0].text])

            product_price.append( [price] )

and later use writerows with s at the end which write every nested list as separated row然后在末尾使用带有swriterows ,它将每个嵌套列表写为单独的行

    csvWriter.writerows(product_price)  # with `s` at the end

BTW: When you write header then you should also use list顺便说一句:当您编写标题时,您还应该使用列表

csvWriter.writerow( ["Price"] )

beause at this moment it treads "Price" as list of chars因为此时它将"Price"作为字符列表

csvWriter.writerow( ["P", "r", "i", "c", "e"] )

and it writes space between chars.它在字符之间写入空格。


EDIT:编辑:

# PEP8: `lower_case_names` for variables `csv_file`, `csv_writer`

with open('Price.csv', 'w', newline='', encoding='utf-8') as csv_file:  
    csv_writer = csv.writer(csv_file, delimiter=' ')
    csv_writer.writerow( ["Price"] )

    for item in items:

        whole_price    = item.find_elements(By.XPATH, './/span[@class="a-price-whole"]')
        fraction_price = item.find_elements(By.XPATH, './/span[@class="a-price-fraction"]')

        if whole_price and fraction_price:

            price = '.'.join([whole_price[0].text, fraction_price[0].text])
            csv_writer.writerow( [price] )

PEP 8 -- Style Guide for Python Code PEP 8——Python 代码风格指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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