#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2017
    Posts
    2
    Rep Power
    0

    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
  2. #2
  3. Maddening Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,453
    Rep Power
    9645
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Location
    uk
    Posts
    4
    Rep Power
    0
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Location
    Minnesota, USA
    Posts
    13
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo