当我从数据中打印一行时,得到以下输出:
"SL","2018-09-30","89.7","-2.1644","5.9884","Max Kepler",596146,518858,"field_out","hit_into_play",,,,,"9","Max Kepler grounds out, shortstop Leury Garcia to first baseman Matt Davidson. ","R","L","R","MIN","CWS","X",6,"ground_ball",1,2,2018,"0.0456","0.4072","0.7193","1.8204","null","null","null",2,8,"Bot","118.48","141.29",,,456078,,"180930_215102","6.8928","-130.3241","-5.0488","-0.9006","26.5098","-26.9291","3.5258","1.6280","60","63.4","6.3970","88.0450","2186","5.2660",531825,518858,456078,571602,660162,570560,544725,547170,641477,594953,"55.2332","0.176","0.145","0.00",1,0,0,2,67,"5","Slider",5,4,5,4,4,5,5,4,"Strategic","Standard"
然后,当我使用此行代码与csv阅读器拆分它时:
csvRow = list(csv.reader(data.splitlines()[1]))
print(csvRow)
这些是我的结果:
[['SL'], ['', ''], ['2018-09-30'], ['', ''], ['89.7'], ['', ''], ['-2.1644'], ['', ''], ['5.9884'], ['', ''], ['Max Kepler'], ['', ''], ['5'], ['9'], ['6'], ['1'], ['4'], ['6'], ['', ''], ['5'], ['1'], ['8'], ['8'], ['5'], ['8'], ['', ''], ['field_out'], ['', ''], ['hit_into_play'], ['', ''], ['', ''], ['', ''], ['', ''], ['', ''], ['9'], ['', ''], ['Max Kepler grounds out, shortstop Leury Garcia to first baseman Matt Davidson. '], ['', ''], ['R'], ['', ''], ['L'], ['', ''], ['R'], ['', ''], ['MIN'], ['', ''], ['CWS'], ['', ''], ['X'], ['', ''], ['6'], ['', ''], ['ground_ball'], ['', ''], ['1'], ['', ''], ['2'], ['', ''], ['2'], ['0'], ['1'], ['8'], ['', ''], ['0.0456'], ['', ''], ['0.4072'], ['', ''], ['0.7193'], ['', ''], ['1.8204'], ['', ''], ['null'], ['', ''], ['null'], ['', ''], ['null'], ['', ''], ['2'], ['', ''], ['8'], ['', ''], ['Bot'], ['', ''], ['118.48'], ['', ''], ['141.29'], ['', ''], ['', ''], ['', ''], ['4'], ['5'], ['6'], ['0'], ['7'], ['8'], ['', ''], ['', ''], ['180930_215102'], ['', ''], ['6.8928'], ['', ''], ['-130.3241'], ['', ''], ['-5.0488'], ['', ''], ['-0.9006'], ['', ''], ['26.5098'], ['', ''], ['-26.9291'], ['', ''], ['3.5258'], ['', ''], ['1.6280'], ['', ''], ['60'], ['', ''], ['63.4'], ['', ''], ['6.3970'], ['', ''], ['88.0450'], ['', ''], ['2186'], ['', ''], ['5.2660'], ['', ''], ['5'], ['3'], ['1'], ['8'], ['2'], ['5'], ['', ''], ['5'], ['1'], ['8'], ['8'], ['5'], ['8'], ['', ''], ['4'], ['5'], ['6'], ['0'], ['7'], ['8'], ['', ''], ['5'], ['7'], ['1'], ['6'], ['0'], ['2'], ['', ''], ['6'], ['6'], ['0'], ['1'], ['6'], ['2'], ['', ''], ['5'], ['7'], ['0'], ['5'], ['6'], ['0'], ['', ''], ['5'], ['4'], ['4'], ['7'], ['2'], ['5'], ['', ''], ['5'], ['4'], ['7'], ['1'], ['7'], ['0'], ['', ''], ['6'], ['4'], ['1'], ['4'], ['7'], ['7'], ['', ''], ['5'], ['9'], ['4'], ['9'], ['5'], ['3'], ['', ''], ['55.2332'], ['', ''], ['0.176'], ['', ''], ['0.145'], ['', ''], ['0.00'], ['', ''], ['1'], ['', ''], ['0'], ['', ''], ['0'], ['', ''], ['2'], ['', ''], ['6'], ['7'], ['', ''], ['5'], ['', ''], ['Slider'], ['', ''], ['5'], ['', ''], ['4'], ['', ''], ['5'], ['', ''], ['4'], ['', ''], ['4'], ['', ''], ['5'], ['', ''], ['5'], ['', ''], ['4'], ['', ''], ['Strategic'], ['', ''], ['Standard']]
如您所见,逗号将被放入列表中自己的索引中。显然,这不是我想要的。我不能只使用split函数,因为某些列的实际数据中包含逗号。
答案 0 :(得分:1)
Map newImmutableMap = ImmutableMap.copyOf(yourMap);
带有一个返回行的Interable。传递字符串(返回单个字符的可迭代字符),使其行为类似于每个字符本身,除非按默认引号字符分组。当到达单引号字符的“行”时,它看起来像两个空白列:csv.reader
。
您没有显示所有代码,但可以执行以下操作:
['', '']
或者,如果您的数据包含在一个连续的字符串中,请使用with open('data.csv',newline='') as f: # newline per csv documentation.
for line in csv.reader(f):
print(line)
一次返回一行:
io.StringIO
输出:
import csv
import io
data = '''\
"SL","2018-09-30","89.7","-2.1644","5.9884","Max Kepler",596146,518858,"field_out","hit_into_play",,,,,"9","Max Kepler grounds out, shortstop Leury Garcia to first baseman Matt Davidson. ","R","L","R","MIN","CWS","X",6,"ground_ball",1,2,2018,"0.0456","0.4072","0.7193","1.8204","null","null","null",2,8,"Bot","118.48","141.29",,,456078,,"180930_215102","6.8928","-130.3241","-5.0488","-0.9006","26.5098","-26.9291","3.5258","1.6280","60","63.4","6.3970","88.0450","2186","5.2660",531825,518858,456078,571602,660162,570560,544725,547170,641477,594953,"55.2332","0.176","0.145","0.00",1,0,0,2,67,"5","Slider",5,4,5,4,4,5,5,4,"Strategic","Standard"
'''
for line in csv.reader(io.StringIO(data)):
print(line)
答案 1 :(得分:0)
读者需要为CSV文件更改定界符。将分隔符指定为在代码中带有单个空格的字符串。
csvRow = list(csv.reader(data.splitlines()[1], delimiter=' '))
print(csvRow)
答案 2 :(得分:0)
尝试使用数据框:
用您的csv源替换test.csv
import pandas as pd
data = pd.read_csv('test.csv', sep=',', delimiter=None, skip_blank_lines=True)
data = data.transpose()