sonofstimpy1
asked on
Python convert long row into multiple columns
I have a CSV file that has just one column but needs to be multiple columns.
Example only one column
row 1 aaaaaaa aaaa aaaaaaaaa aaaaa a aaaaaaa
I want it be with multiple columns
column(fixed length) column2(fixed length) column3(fixed length) column4(fixed length)
and the rows will be parsed into multiple fixed column lengths.
Anybody have an idea to do this in Python?
Example only one column
row 1 aaaaaaa aaaa aaaaaaaaa aaaaa a aaaaaaa
I want it be with multiple columns
column(fixed length) column2(fixed length) column3(fixed length) column4(fixed length)
and the rows will be parsed into multiple fixed column lengths.
Anybody have an idea to do this in Python?
ASKER
Thanks for replying. Lemmie be more specific.
I have a CSV file with 1,000+ rows.
Each row is just a single column, looking something like this...
Here is an example Here is a second example blah blah
I need this row should be cut into multiple columns. I know the fixed length of each column...
column 1 is the first 8 characters
column 2 is the next 4 characters
column 3 is the next 5 characters
column 4 is the next 6 characters
So I want to open the file and read, then write to CSV with the revised rows with columns.
I have a CSV file with 1,000+ rows.
Each row is just a single column, looking something like this...
Here is an example Here is a second example blah blah
I need this row should be cut into multiple columns. I know the fixed length of each column...
column 1 is the first 8 characters
column 2 is the next 4 characters
column 3 is the next 5 characters
column 4 is the next 6 characters
So I want to open the file and read, then write to CSV with the revised rows with columns.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Pepr. I was re-checking your original post and it works exactly how I want it. I've noted your point about CSV files in general. Thanks for taking the time to help.
I am glad if it helped. Anyway, there is another part of the problem when working with fixed-width columns -- the formatting of the value to the width. Feel free to continue in discussion just below if you have the related questions.
Try the following code. The reading part shows how to extract the values from the fixed-width columns, the writing part shows how to write tha values to a CSV file.
Open in new window
The sample of 3 rows was saved to a.txt. It printsOpen in new window
and it writes the following content to the a.csv:Open in new window