回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>问题很简单,我有一个CSV文件,其中有四列,我想为第一列赋值,并在python脚本中将其放入字典中。我不想在字典中添加任务完成日期的值。在</p>
<p>在一个名为VC.csv版,例如:</p>
<pre class="lang-none prettyprint-override"><code>24M Technologies,Series A,8/19/10
24M Technologies,Grant,8/16/10
2B Energy,Private Equity,3/18/14
2B Energy,Series B,3/18/14
2B Energy,Unattributed VC,5/1/08
3GSolar Photovoltaics,Series A,12/17/12
3sun Group,Growth Equity,3/3/14
3Tier Group,Series C,11/17/08
</code></pre>
<p>我想要的最终结果是,当我打印字典时,它们是这样的。在</p>
<p>例如</p>
^{pr2}$
<p>我的问题是尝试循环槽,并在已经定义好的字典中添加更多内容。我猜我不是在追加,而是在每次循环中重新创建和重写循环。结果我得到的结果是<code>{'company': '2B Energy', 'Private Equity': '3/18/14'}</code>我需要代码的最后一行来测试字典是否已经存在;如果已经存在,它将向它追加额外的整数日期。在</p>
<p>这是我的密码。。。在</p>
<pre><code>import csv
companyList =[]
transactionDates=[]
dictNames=[]
def fileNameCleaner(namer):
namer = namer.replace(' ', '')
namer = namer.replace(',','')
namer = namer.replace('-','')
namer = namer.replace('.','')
namer = namer.replace('_','')
namer = namer.replace('@','')
namer = namer.replace('&','')
return namer
with open('VC.csv', 'rb') as rawData:
timelineData = csv.reader(rawData, delimiter=',', quotechar='"') # Open CSV file and snag data
for line in timelineData: # Run through each row in csv
companyList.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(fileNameCleaner(line[0])) # Create list and remove some special charcters
companyList = list(set(companyList)) # Remove duplicates and Sort
for companyListRow in companyList:
with open('VC.csv', 'rb') as rawDataTwo:
timelineDataTwo = csv.reader(rawDataTwo, delimiter=',', quotechar='"')
for TList in timelineDataTwo:
company = TList[0]
finRound = TList[1]
tranDate = TList[2]
if companyListRow == fileNameCleaner(TList[0]):
companyListRow = {'company':TList[0], finRound:tranDate }
print companyListRow
</code></pre>