Reputation: 11
I need to write two different lists to two separate .csvs.
Problem is, my code (below) appends the second list to the first list (even when both don't have the same number of columns) and writes to the .csv. This is repeated exactly in the second .csv.
This is a simulation on Python 3.7 using Anaconda's Spyder 3 IDE on Windows.
...
specEff = switchData = []
...
switchData.append([k, totalAttenData[k][0], rxSite, sameSiteCount, rxFade, totalAttenData[k][1], totalAttenData[k][2]])
...
#%%# Save results
specEff.append([predictAheadMins, aveSpecEff])
if platform == 'win32':
SwitchDataFile = ".\Results\SwDataFilePred2#"+"{:,}".format(predictAheadMins)+"mins.csv"
specEffDataFile = ".\Results\SpEffDataFilePred2.csv"
elif platform == 'linux' or platform == 'linux2':
SwitchDataFile = "./Results/SwDataFilePred2#"+"{:,}".format(predictAheadMins)+"mins.csv"
specEffDataFile = "./Results/SpEffDataFilePred2.csv"
try:
with open(SwitchDataFile, 'w', newline='') as outfile:
csv.writer(outfile).writerows(switchData)
except:
with open(SwitchDataFile, 'a', newline='') as outfile:
csv.writer(outfile).writerow(switchData)
try:
with open(specEffDataFile, 'w', newline='') as outfile2:
csv.writer(outfile2).writerows(specEffData)
except:
with open(specEffDataFile, 'a', newline='') as outfile2:
csv.writer(outfile2).writerows(specEffData)
I expect to have two different csv files storing different lists.
Upvotes: 1
Views: 67
Reputation: 4318
If you initialise a list in one line, it creates one list with two variable names for it:
a = b = []
a.append('a')
print(a)
print(b)
Result:
['a']
['a']
Instead you need to initialize it two times to have two lists:
a, b = [], []
a.append('a')
print(a)
print(b)
Result:
['a']
[]
Upvotes: 3