Hardest problem of the world. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hardest problem of the world.

How to add 64 blank columns at the beginning of a CSV file? By python.

30th Sep 2021, 5:25 AM
Sourya Banerjee
Sourya Banerjee - avatar
5 Answers
30th Sep 2021, 5:41 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
When you say "add 64 blank columns at the beginning of a CSV file", did you mean the 64 blank columns were meant as header line to be named later on, or did you mean to add the 64 blank columns to each lines in the file?
30th Sep 2021, 11:29 AM
Ipang
+ 1
To each line of the file.......
1st Oct 2021, 4:25 AM
Sourya Banerjee
Sourya Banerjee - avatar
+ 1
Sourya Banerjee I have an idea, but I haven't tried to implement it, yet still I think it's worth a shot. Read the entire file raw into a `list`, let's say the `list` was named <content>, maybe by using readlines(). And then close the file. Next prepare the 64 new column new_columns = ', ' * 64 And then you iterate the list using range() for row_id in range( len( content ) ): content[ row_id ] = new_columns + content[ row_id ] This will prepend <new_columns> to each element in the `list`. Finally you open the file for writing mode (not append mode), and save the `list` into the file. Give it a shot! hope it works! 👍
1st Oct 2021, 6:59 AM
Ipang
0
Yes.... it's shared link is useless....it is adding the column at the end..........
30th Sep 2021, 10:57 AM
Sourya Banerjee
Sourya Banerjee - avatar