Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write to CSV
#1
I have a list of values [abc123, abc123, abc123, abc123, etc..]

I want to input that list into a CSV whereby the first 3 characters of each index are in the first column.

i.e 
 list = [abc123, abc123, abc123, abc123]


for i in list:
     first_3 = i[:3]
     the_rest =i[3:]
 

I need the data structured in the cvs like follows:


col1      col2   col3   col4
abc         1      2      3
abc         1      2      3
abc         1      2      3


I've created a csv with headers as follows:

file = open('test.cv', 'w')
file.write('First col')
for h in range(0, 4):
      file.write('Col' + str(h) + ",")
file.write('\n')
I need to iterate through the list but I'm stuck on how to place rows and columns.  Keep getting stuck. The closest to any solution i've got to is this but its way off..

for i in range(0, len(list)
      for j in range(0, len(list[i]):
            file.write(list[i][j] + ","
file.write("\n")
Reply


Messages In This Thread
Write to CSV - by arsenal88 - Apr-25-2017, 08:10 PM
RE: Write to CSV - by volcano63 - Apr-25-2017, 09:30 PM
RE: Write to CSV - by wavic - Apr-26-2017, 12:02 AM
RE: Write to CSV - by sparkz_alot - Apr-26-2017, 01:52 PM
RE: Write to CSV - by snippsat - Apr-26-2017, 02:03 PM
RE: Write to CSV - by sparkz_alot - Apr-26-2017, 02:23 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020