1

I have a list of lists looks like this:

signals_list = [ [1,4,7..... 5,7],[4,-7,1....-5,-8,4],.....]

I want to write these inner lists column wise in a csv file. I have 176 inner lists and 176 column needs to be made. Every inner list has almost 39400 instants.

Csv should look like this:

enter image description here

What i have tried:

writing nested list into csv python

write python list of list into csv python

What i have done

# GETTING ALL THE WAV FILES FROM DIRECTORY AND CONVERTING INTO NUMPY ARRAY
path = "C:/Users/Marwat/.spyder-py3/FYP/input/set_a/"
files = os.listdir(path)
print(len(files))
signals_list = []
for filename in glob.glob(os.path.join(path, '*.wav')):
    sample_rate, data = wavfile.read(filename)
    signals = [np.array(data,dtype=int)]
    signals_list.append(signals)


# WRITING SIGNALS TO CSV FILE
d = [signals_list]
col_name = 1
transposed_signals = zip_longest(*d, fillvalue = '')
with open('C:/Users/Marwat/.spyder-py3/FYP/input/numbers.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:
      wr = csv.writer(myfile)
      wr.writerow('sound_'+ str(col_name))
      wr.writerows(transposed_signals)
      col_name +=1
myfile.close()

OUTPUT

Output for my above code

Any help will be highly appreciated

Noman marwat
  • 321
  • 1
  • 12
  • Can you say of the things you've tried,which gets you _closest_ to your goal, and how it differs from your desired output? Also, I'm not understanding your desired translation to CSV: for example you have a -3 in your list, but it doesn't show anywhere in your CSV. – Nikolas Stevenson-Molnar Aug 29 '19 at 20:41
  • i have edit my questions and -3 was dummy value just to show my requirement – Noman marwat Aug 30 '19 at 06:22
  • ATTENTION: THIS CODE WORKS PERFECTLY FOR ME. THANKS TO ALL OF YOU. 'list = [] for i in range(len(signals_list)): list.append(('sound'+str(i+1))) df = pd.DataFrame(columns=list) for i in range(len(signals_list)): df[list[i]] = pd.Series(signals_list[i][0]) df.to_csv('PATH/TO/FILE.csv',sep=',',index=False) ' – Noman marwat Aug 31 '19 at 17:53

4 Answers4

1

You've almost got it. The two places you are running into problems are:

1) Sticking signals_list inside an outer list. Pass signals_list directly to zip_longest and you should be set there:

transposed_signals = list(zip_longest(*signals_list, fill_value = ''))

2) Writing the header row. First, you need to write all columns in that row (you're just writing one). Also, writerow() expects an iterable, which is why, when you pass a string, you get one character per column. Try this instead, and leave out col_name:

wr.writerow(('sound_' + str(i + 1) for i in range(len(transposed_signals))))

Putting it all together (just the second part), you should have:

transposed_signals = list(zip_longest(*signals_list, fillvalue=''))
with open('C:/Users/Marwat/.spyder-py3/FYP/input/numbers.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:
    wr = csv.writer(myfile)
    wr.writerow(('sound_' + str(i + 1) for i in range(len(transposed_signals))))
    wr.writerows(transposed_signals)

# Calling myfile.close() isn't necessary since `with` does that automatically upon exiting that scope.
Nikolas Stevenson-Molnar
  • 3,001
  • 1
  • 17
  • 25
0

You can transform your list in a numpy array and transpose it

array = np.array(signals_list)
array = array.T

Then use pandas to create a DataFrame and store it as csv

df = pd.DataFrame(array)
df.to_csv('my_signals.csv')
0

zip()

zip() transposes a list*, so rows become columns and columns become rows. Based on the second link, you can do:

import csv

with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(zip(*a))

If this doesn't work for you, please tell me how it doesn't work so I can fix it.

*It doesn't actually transpose a list, it "aggregates elements from each of the iterables", which in this case is essentially the same.

Hiatsu
  • 157
  • 1
  • 6
0

You can create a dataframe, transpose it then set the headers in place and export it to a csv file

import pandas as pd    
signals_list =  [[1,4,7],[4,-7,1],[1,9,8]]
df = pd.DataFrame(data =signals_list ,columns=["a","b","c"]).T
df.columns = df.index.tolist()
df.to_csv("file.csv", index=False)
Daniel
  • 4,119
  • 4
  • 30
  • 45