August 14th, 2017, 01:42 AM
-
how to convert a text file into csv file in perl
i have a ":" separated text file that i have to convert into excel sheet
how can i do that??
my file in in this format
1.0:aaaaa
2.0:bbbbb
5.2:cccc
i want it in an excel sheet
1.0 aaaaaa
2.0 bbbbbb
can someone please help
August 14th, 2017, 02:15 AM
-
Copy and paste into Excel, select all, use Text to Columns to split on the colon.
If you need an automatic method, replace the colons with tabs and give the file a .tsv ("tab-separated values") extension. Excel can open those too.
November 23rd, 2017, 04:45 AM
-
use e.g. open (INFILE, $sourceFile) or die "Couldn't open $file for reading: $!"; --- to open the file
then read each line like this....
while (read(INFILE, $buffer, $num_bytes)) {
use split to split the fields in an array with : separater you have
then use join with , to rejoin them into a new string.
then open a write file called <filename>.csv
then use print to print the new line to the file handle of the write file
then close both files.
November 27th, 2017, 09:43 AM
-
This response has a college lab "smell"; You should never read the whole file into storage, process, and write the whole result to another file. This approach is not scalable. Where the data is already "line oriented", read and process each line individually, writing the line's result to the new file, and repeating to the end of the input file. This approach isn't dependent on the size of the input file; it can handle gigabytes of data without batting an eye. Your way will have system admins knocking at your door wanting to know what you are doing that's using up all the system's memory.