简体   繁体   English

Python中的CSV编写器问题

[英]CSV Writer Issue in Python

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. 我认为这意味着,每次CSV编写器在我的代码中看到“ [”时,都会将随后的输出发送到新的单元格中。

The final code that I want to send to the CSV looks as such... 我想要发送到CSV的最终代码如下所示...

[[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". 而且,我希望每当您看到长数字“ 400827889”时,所有这些较小的列表和字符串都会被换行。

At current, my variable names (line 5) are showing up as follows: 目前,我的变量名(第5行)显示如下:

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: 我的CSV输出如下:“ //”是我在事后添加的表示“新单元格”的内容,如下所示:

 "[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! 感谢您提供的所有帮助!

So, per pythons documentation on csv here , the delimiter is a single character that separates data fields. 因此,根据此处csv上的python文档 ,定界符是用于分隔数据字段的单个字符。 First thing you want to do is to change delimiter="[", to delimiter=",", . 您要做的第一件事是将delimiter="[",更改为delimiter=",", Then change writer.writerows in line 5 to write.writerow . 然后改变writer.writerows行5至write.writerow

You should also have lineterminator = '' be something other than an empty string. 您还应该使lineterminator = ''而不是空字符串。

To be perfectly honest I'm not sure if a csv is your best option for handling your data. 老实说,我不确定csv是否是处理数据的最佳选择。 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. 因为看起来好像您有很多重复数据-您不需要每一行数据都需要行,引用,出席等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM