简体   繁体   English

通过 collums/rows 将数据附加到现有 CSV 文件

[英]Appending data to existing CSV file by collums/rows

I am trying to export data into csv.我正在尝试将数据导出到 csv。 When I export it, it is exported as a singular row and each additional data is appended on the same row.当我导出它时,它被导出为单个行,并且每个附加数据都附加在同一行上。 The result is a very long row which is difficult to navigate.结果是非常长的行,难以导航。

The goal is for each value (Time, Wallets) to have their own collum and each time the script is ran, the new data is appended to the csv under the respective collum.目标是让每个值(时间、钱包)都有自己的 collum,并且每次运行脚本时,新数据都会附加到相应 collum 下的 csv 中。 I have tried guides online but wasn't able to find any solutions.我已经尝试了在线指南,但无法找到任何解决方案。

Here is my code if anyone has any suggestions or ideas.如果有人有任何建议或想法,这是我的代码。 . . Additionally, if anyone has any suggestions for restructuring my code or making it better, I'd love to hear it.此外,如果有人对重组我的代码或使其更好有任何建议,我很想听听。 As a beginner I would like to learn as much as possible.作为初学者,我想尽可能多地学习。

#Open ChromeDriver
PATH = ('/Users/chris/Desktop/chromedriver')
driver = webdriver.Chrome(PATH)

#Get Website 

driver.get("https://bitinfocharts.com/top-100-richest-dogecoin-addresses.html")
driver.implicitly_wait(5)
driver.maximize_window()

#Creates the Time
now = datetime.now()
current_time = now.strftime("%m/%d/%Y, %H:%M:%S ")


#Identify the section of page
time.sleep(3)
page = driver.find_element(By.CSS_SELECTOR,'.table.table-condensed.bb')


#Gather the data
time.sleep(3)
num_of_wallets = page.find_element(By.XPATH, "//html/body/div[5]/table[1]/tbody/tr[10]/td[2]").text

#Write to CSV


table_dict = {"Time":current_time,
            "Wallets":num_of_wallets}

headers = ["Time", "Wallets"]

file = open('dogedata.csv', 'a')
file.write(f'{current_time},{num_of_wallets}')
file.close()

The smallest fix, as Shubham P. pointed out, is to make sure you are writing a "line" by including a line break, like '\n' :正如 Shubham P. 指出的那样,最小的解决方法是通过包含换行符来确保您正在编写“行”,例如'\n'

file = open('dogedata.csv', 'a')
file.write(f'{current_time},{num_of_wallets}\n')
file.close()

The far better fix, as martineau pointed out, is to use the csv module: it's standard, well-developed and used, and importantly takes care of issues like escaping and quoting characters, and it only takes one more line:正如 martineau 所指出的,更好的解决方法是使用csv模块:它是标准的,经过良好开发和使用,重要的是解决了 escaping 和引用字符等问题,并且只需要多行:

file = open('dogedata.csv', 'a', newline='')
writer = csv.writer(f)
writer.writerow([current_time, num_of_wallets])
file.close()

Note that newline='' was added to the open() function, which tells open() not to handle newlines, and instead defer to the writer which has CSV-specific smarts for dealing w/newlines.请注意, newline=''被添加到open() function 中,它告诉 open() 不要处理换行符,而是遵从具有 CSV 特定智能处理 w/换行符的编写

Also, instead of using string formatting, you just wrap your data in a list ( [current_time, num_of_wallets] );此外,您只需将数据包装在一个列表中( [current_time, num_of_wallets] ),而不是使用字符串格式; the writer will convert everything to strings/text.作者会将所有内容转换为字符串/文本。

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

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