1

I can't get the output I have scraped using Beautiful Soup to write to a CSV file. I have it in the format but I like when I print, but cannot get it to write to a csv like that.

I've tried several variations of the csv write function but most of the things I can find online are for a given list or dictionary, none of which is the format of my data. Maybe I need to make it a list somehow?

rank = (pga_text)[0]
player = (pga_text)[2]
sg_p = (pga_text)[4]
pga_file = rank + ", " + player + ", " + sg_p
print(pga_file)

myfile = open(r'C:\Users\Walsh\Documents\Python Scripts\pga.csv', 'w')
with myfile:
    myfields = [rank]
writer = csv.writer(myfile)
writer.writerow(pga_file)
a.close()

As mentioned, the output from the print is in the format I wanted:

1, Justin Harding, 1.000
2, Andrew Putnam, .952
3, Dominic Bozzelli, .821
4, Graeme McDowell, .789

but nothing writes to the actual file.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    fixed the indentation – eyllanesc Jun 20 '19 at 00:57
  • What is the variable "a" that you use a.close(), is this a typo? – Simon Hobbs Jun 20 '19 at 01:00
  • There's no need to put variables in parentheses like `(pga_text)[0]`. Just write `pga_text[0]`. – Barmar Jun 20 '19 at 01:10
  • Thanks for the tip on the variable and parentheses. That's been fixed. The a.(close) is probably a typo at this point. I've been trying to diagnose with several different code types. I can change that to myfile.close in the example to actually close the file. Thanks for the comments! – Kevin Michael Jun 20 '19 at 02:11

5 Answers5

0

Based on this example it appears as though you are writing the string pga_file.

You want to write a row as a list type.

rank = (pga_text)[0]
player = (pga_text)[2]
sg_p = (pga_text)[4]
pga_file = [rank, player, sg_p]
print(pga_file)

myfile = open(r'C:\Users\Walsh\Documents\Python Scripts\pga.csv', 'w')
with myfile:
    myfields = [rank]
writer = csv.writer(myfile)
writer.writerow(pga_file)
a.close()

In this example it also appears as though you are not using myfields.

See: csv.writer documentation

Zorba
  • 1
  • 1
  • 1
0

I think you have a problem with the with.

with will clean up its associated resources when execution flow exits with scope. That's to say, in your orignal code, when you called writer.writerow(pga_file), the file associated with with had been closed. Try something like this:

import csv

pga_file = '1, Justin Harding, 1.000'
myfile = open(r'pga.csv', 'w')
with myfile:
    writer = csv.writer(myfile)
    writer.writerow(pga_file)

Regarding to with, you may refer to what-is-the-python-keyword-with-used-for.

fishoverflow
  • 91
  • 1
  • 5
0

I you dont want to import csv library, you can do this way also

rank = 1
player = "Justin Harding"
sg_p = 1.000
pga_file = str(rank) + ", " + player + ", " + str(sg_p) + "\n"

myfile = open('pga.csv', 'w')
myfile.write(pga_file)
myfile.close()
Eka
  • 14,170
  • 38
  • 128
  • 212
0

Given that you already have the string you want to write to the csv file, you could use this helper method. (Given that pga_file is of string type). I've used this simple writer with different extensions and just tested it out with csv files and it worked.

def save (filename, pga_file):
  f = open(filename, 'w')
  f.write(pga_file)
  f.close()

where filename is 'C:\Users\Walsh\Documents\Python Scripts\pga.csv'

0
import csv

f = open('test.csv', mode='w')

with f as test_file:
    test_writer = csv.writer(test_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    test_writer.writerow(['John', 'Smith', 'November'])
    test_writer.writerow(['Frank', 'Mueller', 'March'])

print('file created')

does this test code work in your environment?

harry hartmann
  • 351
  • 1
  • 9