I'm learning about local I/O and how to read and write files. I'm currently working on an assignment, where I have to parse through a semi-colon separated file, convert the semicolons to commas, and replace any values that have commas in them with semicolons. To give you a better idea, here's a piece of the raw data I'm working with.
String;Categorical;Categorical;Int;Int;Int;Int;Float;Float;Int;Int;Int;Int;Float;Float;Float
100% Bran;N;C;70;4;1;130;10;5;6;280;25;3;1;0.33;68.402973
100% Natural Bran;Q;C;120;3;5;15;2;8;8;135;0;3;1;1;33.983679
All-Bran;K;C;70;4;1;260;9;7;5;320;25;3;1;0.33;59.425505
All-Bran with Extra Fiber;K;C;50;4;0;140;14;8;0;330;25;3;1;0.5;93.704912
Almond Delight;R;C;110;2;2;200;1;14;8;-1;25;3;1;0.75;34.384843
Apple Cinnamon Cheerios;G;C;110;2;2;180;1.5;10.5;10;70;25;1;1;0.75;29.509541
Froot Loops;K;C;110;2;1;125;1;11;13;30;25;2;1;1;32.207582
Frosted Flakes;K;C;110;1;0;200;1;14;11;25;25;1;1;0.75;31.435973
Frosted Mini-Wheats;K;C;100;3;0;0;3;14;7;100;25;2;1;0.8;58.345141
Fruit & Fibre Dates, Walnuts, and Oats;P;C;120;3;2;160;5;12;10;200;25;3;1.25;0.67;40.917047
The goal is to separate the values with commas. For any values that have a comma in them, such as the last value - "Fruit & Fibre Dates, Walnuts, and Oats", I want to replace those commas with semicolons. I cannot import any helper libraries, such as csv or pandas. I'm not sure how to do this assignment, but here is the code I have so far:
def convert_table(filename_in, filename_out):
with open('cereal.scsv', 'r') as filename_in:
for line in filename_in:
print(line, end='\n')
with open('cereal.scsv', 'w') as filename_out:
for line in filename_in:
newLine = line.replace(";", ",")
filename_out.write(newLine)
return True
Any advice or tips are much appreciated!