I scrape a website with Python 3.5 (BeautifulSoup) and the result is a list. The values are stored in a variable called "project_titles".
The values look like:
project_titles = ['I'm Back. Raspberry Pi unique Case for your Analog Cameras', 'CitizenSpring - App to crowdsource & map safe drinking water', 'Shoka Bell: The Ultimate City Cycling Tool']
I want to split the values at the comma and export this to an Excel file or CSV.
I need the values in an Excel like:
Since you already have a list of strings conforming to the columns required in your CSV file you can simply write the list out using the csv
module:
import csv
project_titles = ["I'm Back. Raspberry Pi unique Case for your Analog Cameras", 'CitizenSpring - App to crowdsource & map safe drinking water', 'Shoka Bell: The Ultimate City Cycling Tool']
with open('projects.csv', 'w') as f:
csv.writer(f).writerow(project_titles)
After running this code the output CSV file would contain:
I'm Back. Raspberry Pi unique Case for your Analog Cameras,CitizenSpring - App to crowdsource & map safe drinking water,Shoka Bell: The Ultimate City Cycling Tool
which you can import into Excel.
Since project_titles
is already a list containing the strings you want, it's easy to use pandas
(can be installed together with one of the scientific Python distributions, see scipy.org ) with the following short code:
import pandas as pd
project_titles = ["I'm Back. Raspberry Pi unique Case for your Analog Cameras",
'CitizenSpring - App to crowdsource & map safe drinking water',
'Shoka Bell: The Ultimate City Cycling Tool']
d = pd.DataFrame(project_titles)
writer = pd.ExcelWriter('data.xlsx')
d.to_excel(writer, 'my_data', index=False, header=False)
writer.save()
You can try a very simple code
project_titles = ["I'm Back. Raspberry Pi unique Case for your Analog Cameras", 'CitizenSpring - App to crowdsource & map safe drinking water', 'Shoka Bell: The Ultimate City Cycling Tool']
with open('data.csv',"w") as fo:
fo.writelines(",".join(project_titles))
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.