如何在CSV文件中写两列?第一个应该具有data [0],第二个应该具有data [1]
with open('list_of_courses.csv', 'w', newline='', delimiter=',') as f:
thewriter = csv.writer(f)
for dept_courses in dept_url_dict.values():
newpage = requests.get("https://bulletin.temple.edu"+dept_courses)
courses = BeautifulSoup(newpage.content, 'html.parser')
courselist = courses.select('p.courseblocktitle')
print(dept_courses)
for c in courselist:
string = c.text
data = string.split(".")
thewriter.writerow(data[0]+","+data[1])
我希望CSV文件具有两列,但当前每个字符都有一列。
最佳答案
尝试这个...
with open('list_of_courses.csv', 'w', newline='', delimiter=',') as f:
thewriter = csv.writer(f)
for dept_courses in dept_url_dict.values():
newpage=requests.get("https://bulletin.temple.edu"+dept_courses)
courses = BeautifulSoup(newpage.content, 'html.parser')
courselist = courses.select('p.courseblocktitle') print(dept_courses)
for c in courselist:
string = c.text
data = string.split(".")
thewriter.writerow([data[0],data[1]])
您应该将列表传递给
thewriter
,而不是字符串。关于python - 如何使用Python 3在两列中将值写入CSV文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56160339/