136

I have data which is being accessed via http request and is sent back by the server in a comma separated format, I have the following code :

site= 'www.example.com'
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(site,headers=hdr)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
soup = soup.get_text()
text=str(soup)

The content of text is as follows:

april,2,5,7
may,3,5,8
june,4,7,3
july,5,6,9

How can I save this data into a CSV file. I know I can do something along the lines of the following to iterate line by line:

import StringIO
s = StringIO.StringIO(text)
for line in s:

But i'm unsure how to now properly write each line to CSV

EDIT---> Thanks for the feedback as suggested the solution was rather simple and can be seen below.

Solution:

import StringIO
s = StringIO.StringIO(text)
with open('fileName.csv', 'w') as f:
    for line in s:
        f.write(line)
4
  • 2
    It's already a CSV, you just have to write each line to a file...
    – icedwater
    May 18, 2016 at 3:51
  • 2
    I'm not sure you even need the StringIO import, to be honest. Also, the solution as-is probably does not separate the lines, as f.write() does not append newlines automatically.
    – icedwater
    May 18, 2016 at 7:48
  • 1
    @icedwater I understand what your saying, but I ran the code above and it was able to properly store the data to a csv file. May 18, 2016 at 16:10
  • 1

6 Answers 6

239

General way:

##text=List of strings to be written to file
with open('csvfile.csv','wb') as file:
    for line in text:
        file.write(line)
        file.write('\n')

OR

Using CSV writer :

import csv
with open(<path to output_csv>, "wb") as csv_file:
        writer = csv.writer(csv_file, delimiter=',')
        for line in data:
            writer.writerow(line)

OR

Simplest way:

f = open('csvfile.csv','w')
f.write('hi there\n') #Give your csv text here.
## Python will convert \n to os.linesep
f.close()
5
  • 25
    for python 3, change it to with open(<path to output_csv>, "w", newline='') as csv_file: Jul 18, 2018 at 10:33
  • 3
    Missing information on this line for line in data:. Please fix that. Thank you. Oct 1, 2018 at 19:38
  • 1
    how to append a line, if there is already something in the csv-file, using the third solution?
    – Jürgen K.
    Sep 12, 2019 at 13:32
  • 7
    @JürgenK. Use 'a'(append mode) in place of 'w'(write mode).
    – Ani Menon
    Sep 19, 2019 at 4:12
  • If you're getting the error: TypeError: a bytes-like object is required, not 'str', just change the 'wb' to 'w' in the open() function.
    – tsveti_iko
    Sep 29, 2020 at 13:09
20

You could just write to the file as you would write any normal file.

with open('csvfile.csv','wb') as file:
    for l in text:
        file.write(l)
        file.write('\n')

If just in case, it is a list of lists, you could directly use built-in csv module

import csv

with open("csvfile.csv", "wb") as file:
    writer = csv.writer(file)
    writer.writerows(text)
13

I would simply write each line to a file, since it's already in a CSV format:

write_file = "output.csv"
with open(write_file, "wt", encoding="utf-8") as output:
    for line in text:
        output.write(line + '\n')

I can't recall how to write lines with line-breaks at the moment, though :p

Also, you might like to take a look at this answer about write(), writelines(), and '\n'.

1
  • 1
    also care about encoding like this open(write_file, 'wt', encoding='utf-8')
    – Bellash
    Sep 28, 2021 at 19:44
9

In addition to the previous answers, I created a class for quickly writing to CSV files. This approach simplifies the management and closure of open files, as well as ensures consistency and cleaner code, especially when dealing with multiple files.

class CSVWriter():

    filename = None
    fp = None
    writer = None

    def __init__(self, filename):
        self.filename = filename
        self.fp = open(self.filename, 'w', encoding='utf8')
        self.writer = csv.writer(self.fp, delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL, lineterminator='\n')

    def close(self):
        self.fp.close()

    def write(self, *args):
        self.writer.writerow(args)

    def size(self):
        return os.path.getsize(self.filename)

    def fname(self):
        return self.filename

Example usage:

mycsv = CSVWriter('/tmp/test.csv')
mycsv.write(12,'green','apples')
mycsv.write(7,'yellow','bananas')
mycsv.close()
print("Written %d bytes to %s" % (mycsv.size(), mycsv.fname()))

Have fun

0
2

What about this:

with open("your_csv_file.csv", "w") as f:
    f.write("\n".join(text))

str.join() Return a string which is the concatenation of the strings in iterable. The separator between elements is the string providing this method.

0

In my situation...

  with open('UPRN.csv', 'w', newline='') as out_file:
    writer = csv.writer(out_file)
    writer.writerow(('Name', 'UPRN','ADMIN_AREA','TOWN','STREET','NAME_NUMBER'))
    writer.writerows(lines)

you need to include the newline option in the open attribute and it will work

https://www.programiz.com/python-programming/writing-csv-files

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.