0

My code is clearly messed up but several tries have led me nowhere. I created a custom dialect as such...

def wereofftoseesv(start_id, end_id):
    with open('nba_2015_16_pbp.csv', "w") as f:
        csv.register_dialect('scraper', delimiter="[", lineterminator = '', escapechar='', quoting=csv.QUOTE_MINIMAL)
        writer = csv.writer(f, dialect='scraper')
        writer.writerows(["gameid", "time_remaining", "entry", "score", "team", "line", "attendance + capacity", "refs"])
        writer.writerows(list_cleaner(start_id, end_id))

I thought what this meant is that every time the CSV writer saw a "[" in my code, is would send the output that follows into a new cell.

The final code that I want to send to the CSV looks as such...

[[400827889], [([48, 0], 'Timofey Mozgov vs. Pau Gasol (Derrick Rose gains possession)', '0 - 0', 'CHI')], ['CHI -1.5'], ['Attendance: 21,957', 'Capacity: 20,917'], ['Mark Ayotte, Scott Foster, Ben Taylor'], [400827889], [([47, 34],

And I would like a new line with all those smaller lists and strings split every time you see the long number "400827889".

At current, my variable names (line 5) are showing up as follows:

g[a[m[e[i[d]     t[i[m[e[_[r[e[m[a[i[n[i[n[g]    e[n[t[r[y]  s[c[o[r[e]  t[e[a[m]    l[i[n[e]    a[t[t[e[n[d[a[n[c[e[" "[+[" "[c[a[p[a[c[i[t[y]  r[e[f[s]

And my CSV outputs, with "//" being a post-facto addition by me to represent "new cell", are as follows:

 "[400827888]"["[([48 //  0] //  'Andre Drummond vs. Al Horford (Ersan Ilyasova gains possession)'//  '0 - 0' //  'ATL')]"["['ATL -6.5']"["['Attendance: 19 // 187' //  'Capacity: 18 // 729']"["['Eli Roe //  Zach Zarba //  Michael Smith']"["[400827888]"["[([47..."

Thanks for any and all help!

1
  • You'll notice that for the output, the first list of lists "[[400827889]..." is never resolved. That's because each one of those outside lists represents one game. I'm hoping to just have every all games in a single CSV output, each line being a single line in each game. Commented Jul 17, 2016 at 17:13

1 Answer 1

1

So, per pythons documentation on csv here, the delimiter is a single character that separates data fields. First thing you want to do is to change delimiter="[", to delimiter=",",. Then change writer.writerows in line 5 to write.writerow.

You should also have lineterminator = '' be something other than an empty string.

To be perfectly honest I'm not sure if a csv is your best option for handling your data. As it is it looks like you have a lot of repeating data - you don't need the line, refs, attendance, etc for every line of data.

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

2 Comments

That is a possibility -- just reproducing the non-repeating data in a CSV and the other supplemental repeating stuff elsewhere. Any suggestions for other options besides CSV? Thanks for your help
This may be overkill but you could use a relational database. Python has a pretty good sqlite api.

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.