【问题标题】:Export in excel will not properly show data在excel中导出将无法正确显示数据
【发布时间】:2017-01-20 16:47:15
【问题描述】:

我必须将一些数据导出到 excel,但我希望它根据类别(用户名、时间、正文、标签、链接)将列表导出到列中

我在这里缺少什么,因为我把所有东西都排成一排。

lists =[]
lists2=[]
lists3=[]
lists4=[]
lists5=[]
for i in range(0,33):
    link = (df.link.iloc[i])
    source1 = urllib.request.urlopen(link).read()
    soup1 = bs.BeautifulSoup(source1,'lxml')
    for username in soup1.find_all('div', class_="user-name"):
        lists.append([username.get_text().strip()])
    for time in soup1.find_all('div',class_="thread-ago"):
        lists2.append([time.get_text().strip()])
    for body in soup1.find_all('div',class_="thread-body"):
        lists3.append([body.get_text().strip()])
    for tags in soup1.find_all('div', class_="data-tag"):
        lists4.append([tags.get_text().strip()])
    for inlink in soup1.find_all('a',class_="thread-detail"):
        lists5.append([inlink.get('href')])

rows = zip(lists,lists2,lists3, lists4,lists5)        

example = open('generalinfo.csv','w')
for column in rows:
    wr = csv.writer(example,quoting = csv.QUOTE_ALL,delimiter = ",")        
    wr.writerows(column)    
example.close()  

【问题讨论】:

    标签: python csv export-to-excel export-to-csv


    【解决方案1】:

    试试这个

    lists =[]
    lists2=[]
    lists3=[]
    lists4=[]
    lists5=[]
    for i in range(0,33):
        link = (df.link.iloc[i])
        source1 = urllib.request.urlopen(link).read()
        soup1 = bs.BeautifulSoup(source1,'lxml')
        for username in soup1.find_all('div', class_="user-name"):
            lists.append([username.get_text().strip()])
        for time in soup1.find_all('div',class_="thread-ago"):
            lists2.append([time.get_text().strip()])
        for body in soup1.find_all('div',class_="thread-body"):
            lists3.append([body.get_text().strip()])
        for tags in soup1.find_all('div', class_="data-tag"):
            lists4.append([tags.get_text().strip()])
        for inlink in soup1.find_all('a',class_="thread-detail"):
            lists5.append([inlink.get('href')])
    
    rows = zip(lists,lists2,lists3, lists4,lists5)  
    with open('generalinfo.csv','w') as csvfile:
        wr = csv.writer(csvfile,quoting = csv.QUOTE_ALL,delimiter = ",")  
        for row in rows:              
            wr.writerows([row]) 
    

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 2022-01-26
      相关资源
      最近更新 更多