繁体   English   中英

如何在CSV(Python)中的不同行和列中编写字符串

[英]How to write strings in different row and columns in a CSV (Python)

我正在尝试从网站上抓取一些数据,我可以实际获取它们,但是它们是用2个不同的字符串编写的,就像我的.csv文件中那样:

aaa
bbb
ccc

和另一个:

xxx
yyy
zzz

我想按照以下格式编写它们:

aaa | xxx
bbb | yyy
ccc | zzz

这是我到目前为止编写的代码:

# import libraries
import urllib2
from bs4 import BeautifulSoup
import csv  
i =0

# specify the url 
quote_page = 'http://www.alertepollens.org/gardens/garden/1/state/'

# query the website and return the html to the variable 'page'
response = urllib2.urlopen(quote_page)

# parse the html using beautiful soap and store in variable `soup`
soup = BeautifulSoup(response, 'html.parser')
test = soup
with open('allergene.csv', 'w') as csv_file:
    writer = csv.writer(csv_file)

    pollene = (("".join(soup.strings)[65:]).encode('utf-8')).replace(' ','').replace('\n',' ').replace('    ',' ').replace('    ',' ').replace(' ','\n')
    print pollene

    state = (([img['alt'] for img in soup.find_all('img', alt=True)])).
    print state.encode
    polen = ''.join(pollene)
    for item in state:
        writer.writerow([item])
    for item2 in pollene:
        writer.writerow([item2])

主要问题之一是我有法语字符(é,ù,à等),并且使用“ strip()”不能正确显示这些字符。

你知道我该怎么做吗?

import csv
with open('a.csv') as a, open('x.csv') as x, open('out.csv', 'w', newline='') as out:
    a_lines = [line.strip()for line in a]
    x_lines = [line.strip()for line in x]
    rows = zip(a_lines, x_lines)
    writer = csv.writer(out, delimiter='|')
    writer.writerows(rows)

出:

aaa|xxx
bbb|yyy
ccc|zzz

a.csv是您的第一个csv文件, x.csv是您的第二个csv文件, out.csv是输出文件。

暂无
暂无

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

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