Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

python - When writing to a csv file, why is each letter in a column?

The code I am using:

import urllib2
import csv
from bs4 import BeautifulSoup

url = "http://en.wikipedia.org/wiki/List_of_ongoing_armed_conflicts"
soup = BeautifulSoup(urllib2.urlopen(url))

fl = open('locations.csv', 'w')

def unique(countries):
    seen = set()
    for country in countries:
        l = country.lower()
        if l in seen:
            continue
        seen.add(l)
        yield country


locs = []
for row in soup.select('table.wikitable tr'):
    cells = row.find_all('td')
    if cells:
        for location in cells[3].find_all(text=True):
            locs.extend(location.split())

locs2 = []            
for locations in unique(locs):
    locations = locs2.extend(locations.split())
print sorted(locs2)

writer = csv.writer(fl)
writer.writerow(['location'])
for values in sorted(locs2):
    writer.writerow(values)

fl.close()

When I print the code I am writing I get a u' in front of each element which I think is why it is outputting this way. I tried using .strip(u'') but it gives me an error that .strip cannot be used as it is a list. What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

locs2 is a list with strings, not a list of lists. As such you are trying to write individual strings as a row:

for values in sorted(locs2):
    writer.writerow(values)

Here values is a string, and writerow() treats it as a sequence. Each element of whatever sequence you pass to that function will be treated as a separate column.

If you wanted to write all locations as one row, pass the whole list to writer.writerow():

writer.writerow(sorted(locs2))

If you wanted to write a new row for each individual location, wrap it in a list first:

for location in sorted(locs2):
    writer.writerow([location])

You don't need to string u prefixes from strings; that's just Python telling you you have Unicode string objects, not byte string objects:

>>> 'ASCII byte string'
'ASCII byte string'
>>> 'ASCII unicode string'.decode('ascii')
u'ASCII unicode string'

See the following information if you want to learn more about Python and Unicode:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...