Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python3在两列中向CSV文件写入值?_Python_Csv - Fatal编程技术网

如何使用Python3在两列中向CSV文件写入值?

如何使用Python3在两列中向CSV文件写入值?,python,csv,Python,Csv,如何在CSV文件中写入两列?第一个应该有数据[0],第二个应该有数据[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"+d

如何在CSV文件中写入两列?第一个应该有数据[0],第二个应该有数据[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]])


您应该将列表传递给编写器,而不是字符串。

试试这个

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]])



您应该将列表传递给编写器,而不是字符串。

您应该将列表传递给编写器,而不是传递由“
”、“
分隔的字符串。尝试使用Writer.writerow([data[0],data[1]])的可能重复项。您应该将
列表
传递给
writerow
函数,而不是传递由
分隔的字符串,'
。请尝试使用writer.writerow([data[0],data[1]])可能重复的