1

I am writing links into a CSV file but the output seems wrong. It separates each character with a comma. Below is my code. What could be wrong here?

import csv

links = [
    'https://www.pickles.com.au/cars/item/-/details/CP-04-16--Built-12-15--Volkswagen--Caddy--2KN-MY16-TSI160-SWB-Runner--Van--2-Seats--4-Doors/980483287',
    'https://www.pickles.com.au/cars/item/-/details/CP-01-17--Built-09-16--Holden--Captiva--CG-MY16-LTZ-AWD--Wagon--7-Seats--5-Doors/980483305',
    'https://www.pickles.com.au/cars/item/-/details/CP-09-16--Built-06-16--Holden--Colorado--RG-MY16-LS-Crew-Cab--Cab-Chassis-Dual-Cab--5-Seats--4-Doors/302151687',
    'https://www.pickles.com.au/cars/item/-/details/CP-07-16--Volkswagen--Amarok--2H-MY16-TDI420-4x2--Utility-Dual-Cab--5-Seats--4-Doors/502529815',
]

with open('f.csv', 'a+', newline='', encoding='utf-8') as w:
    r = csv.writer(w)
    for link in links:
        r.writerow(link)

Output:

h,t,t,p,s,:,/,/,w,w,w,.,p,i,c,k,l,e,s,.,c,o,m,.,a,u,/,c,a,r,s,/,i,t,e,m,/,-,/,d,e,t,a,i,l,s,/,C,P,-,0,4,-,1,6,-,-,B,u,i,l,t,-,1,2,-,1,5,-,-,V,o,l,k,s,w,a,g,e,n,-,-,C,a,d,d,y,-,-,2,K,N,-,M,Y,1,6,-,T,S,I,1,6,0,-,S,W,B,-,R,u,n,n,e,r,-,-,V,a,n,-,-,2,-,S,e,a,t,s,-,-,4,-,D,o,o,r,s,/,9,8,0,4,8,3,2,8,7
h,t,t,p,s,:,/,/,w,w,w,.,p,i,c,k,l,e,s,.,c,o,m,.,a,u,/,c,a,r,s,/,i,t,e,m,/,-,/,d,e,t,a,i,l,s,/,C,P,-,0,1,-,1,7,-,-,B,u,i,l,t,-,0,9,-,1,6,-,-,H,o,l,d,e,n,-,-,C,a,p,t,i,v,a,-,-,C,G,-,M,Y,1,6,-,L,T,Z,-,A,W,D,-,-,W,a,g,o,n,-,-,7,-,S,e,a,t,s,-,-,5,-,D,o,o,r,s,/,9,8,0,4,8,3,3,0,5
h,t,t,p,s,:,/,/,w,w,w,.,p,i,c,k,l,e,s,.,c,o,m,.,a,u,/,c,a,r,s,/,i,t,e,m,/,-,/,d,e,t,a,i,l,s,/,C,P,-,0,9,-,1,6,-,-,B,u,i,l,t,-,0,6,-,1,6,-,-,H,o,l,d,e,n,-,-,C,o,l,o,r,a,d,o,-,-,R,G,-,M,Y,1,6,-,L,S,-,C,r,e,w,-,C,a,b,-,-,C,a,b,-,C,h,a,s,s,i,s,-,D,u,a,l,-,C,a,b,-,-,5,-,S,e,a,t,s,-,-,4,-,D,o,o,r,s,/,3,0,2,1,5,1,6,8,7
h,t,t,p,s,:,/,/,w,w,w,.,p,i,c,k,l,e,s,.,c,o,m,.,a,u,/,c,a,r,s,/,i,t,e,m,/,-,/,d,e,t,a,i,l,s,/,C,P,-,0,7,-,1,6,-,-,V,o,l,k,s,w,a,g,e,n,-,-,A,m,a,r,o,k,-,-,2,H,-,M,Y,1,6,-,T,D,I,4,2,0,-,4,x,2,-,-,U,t,i,l,i,t,y,-,D,u,a,l,-,C,a,b,-,-,5,-,S,e,a,t,s,-,-,4,-,D,o,o,r,s,/,5,0,2,5,2,9,8,1,5

The output CSV file screenshot. enter image description here

2 Answers 2

3

Solution:

Try replacing:

    r.writerow(link)

With:

    r.writerow([link])

So that your full code would become:

with open('f.csv', 'a+', newline='', encoding='utf-8') as w:
    r = csv.writer(w)
    for link in links:
        r.writerow([link])

Or also:

    r.writerow(link,)

Would work, that would make it a single element tuple which also works.

Explanation:

The reason it gets treated to be multiple columns is because a string is an iterable, that said it would get treated as list(link) which would give a list of all the characters of link separated. So making it a single element list would stop that from happening.

Sign up to request clarification or add additional context in comments.

Comments

1

To expand the @U12-Forward answer - instead of iterating over links list and construct one-element list/tuple from each link, you can just use csv.csvwriter.writerows() method

import csv

links = [
    'https://www.pickles.com.au/cars/item/-/details/CP-04-16--Built-12-15--Volkswagen--Caddy--2KN-MY16-TSI160-SWB-Runner--Van--2-Seats--4-Doors/980483287',
    'https://www.pickles.com.au/cars/item/-/details/CP-01-17--Built-09-16--Holden--Captiva--CG-MY16-LTZ-AWD--Wagon--7-Seats--5-Doors/980483305',
    'https://www.pickles.com.au/cars/item/-/details/CP-09-16--Built-06-16--Holden--Colorado--RG-MY16-LS-Crew-Cab--Cab-Chassis-Dual-Cab--5-Seats--4-Doors/302151687',
    'https://www.pickles.com.au/cars/item/-/details/CP-07-16--Volkswagen--Amarok--2H-MY16-TDI420-4x2--Utility-Dual-Cab--5-Seats--4-Doors/502529815',
]

with open('f.csv', 'a+', newline='', encoding='utf-8') as w:
    r = csv.writer(w)
    r.writerows(links)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.