Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Convert csv to txt?

    Hello, guys

    I have a idea.
    I want to take some field from two csv files and put in one txt file.

    Do you know some way?

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    40
    Thanks
    0
    Thanked 7 Times in 7 Posts
    both CSVs and TXTs are plain-text formats.

    You should be able to open a file to write to, open 1 of the CSVs, dump it's entire content to the TXT, then open the 2nd CSV, dump it's contents to the TXT, and... that's it. you should now have your concatenated TXT.



    Edit:

    Unless you mean you want *parts* of each CSV. In which case, you'd do the same general thing, but instead of writing directly tot he TXT, you'd do something like

    Code:
    get_line_of_data_from_csv;
    if(conditions_for_each_line){
    write to txt file;
    }
    Last edited by themousemaster; 02-23-2012 at 10:50 PM.

  • Users who have thanked themousemaster for this post:

    krasi_e_d (02-24-2012)

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by themousemaster View Post
    both CSVs and TXTs are plain-text formats.

    You should be able to open a file to write to, open 1 of the CSVs, dump it's entire content to the TXT, then open the 2nd CSV, dump it's contents to the TXT, and... that's it. you should now have your concatenated TXT.



    Edit:

    Unless you mean you want *parts* of each CSV. In which case, you'd do the same general thing, but instead of writing directly tot he TXT, you'd do something like

    Code:
    get_line_of_data_from_csv;
    if(conditions_for_each_line){
    write to txt file;
    }
    Yes! Exactly I want *parts* of each CSV and writing to TXT. I'll try it and keep it touch.

  • #4
    New Coder
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I need help, please!
    I try to read from this file:

    file.csv

    first row:
    number,type,postion,number-doc,cost,cost1,cost2,name,note,note1,address,type1

    This is second row:
    108826,ID,1,0000111366,160.08,32.02,192.1,PREZ,325352,B325352,str.pret,paid

    This is third row
    108830,ID,1,1002304974,872.33,174.46,1046.79,OVM,23532532,p23532532,hambur,paid

    and write to another. I want to write after first row sometimes there are more, then I want to replace ',' with '#' and I want to take only

    ID#0000111366#PREZ#str.pret#paid
    ID#1002304974#OVM#hambur#paid

    I try with this code:
    PHP Code:
    $section file_get_contents('file.csv');
    $File "file.txt"
    $File str_replace(',','#',$File); 
     
    $Handle fopen($File'w');
     
    $Data $section
       
    fwrite($Handle$Data);
     print 
    "Data Written"
     
    fclose($Handle); 
    but I written everything.
    Last edited by krasi_e_d; 02-26-2012 at 12:39 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Here is a start - I'll let you finish it...
    Code:
    <?PHP
    $lines = file('file.csv');
    $c_lines = count($lines);
    $i = 1;
    for($i=1;$i<$c_lines;$i++) {
    	$array01 = explode(",", $lines[$i]);
    	$new_line = "ID#" . $array01[3]  . "#" . $array01[7]  . "#" . $array01[10]  . "#" . $array01[11];
    	$data = $data . $new_line . "\n";
    }
    echo nl2br($data);
    ?>

  • Users who have thanked litebearer for this post:

    krasi_e_d (02-26-2012)

  • #6
    New Coder
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by litebearer View Post
    Here is a start - I'll let you finish it...
    Code:
    <?PHP
    $lines = file('file.csv');
    $c_lines = count($lines);
    $i = 1;
    for($i=1;$i<$c_lines;$i++) {
    	$array01 = explode(",", $lines[$i]);
    	$new_line = "ID#" . $array01[3]  . "#" . $array01[7]  . "#" . $array01[10]  . "#" . $array01[11];
    	$data = $data . $new_line . "\n";
    }
    echo nl2br($data);
    ?>
    litebearer with this code i miss second line, sory for my stupid replay.

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    show your most recent code

  • #8
    New Coder
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by litebearer View Post
    show your most recent code
    fakt.csv
    ID_PURCHASE_INVOICE,DOC_TYPE_NAME,DOC_EXT_CODE,DOC_NUM,DOC_DATE,WOVAT_AMOUNT,VAT_AMOUNT,TOTAL_AMOUNT ,CONTR
    108826,fakt,1,0000111366,2/15/2012,160.08,32.02,192.1,prezis,103565237,BG103565237,paris,,,,,,,,,,,,paid,,,,,,,,,,
    108830,fakt,1,1002304974,2/15/2012,872.33,174.46,1046.79,ovm,121759222,BG121759222,,,hamburg,,,,,,,,,,paid,,,,,,,,,,

    delivery_head.csv
    ID_DOC,DOC_NUM,DOC_DATE,ID_CONTRAGENT,ID_PURCHASE_INVOICE,ID_INVENT,ID_OPERATION,IS_ARCHIVE
    1016853,11281,2/16/2012,103000090,108826,10133,1,0
    1016858,11286,2/16/2012,103000221,108830,10145,1,0

    invent.csv
    ID_INVENT,INVENT_NAME,ACCOUNT_TAG
    10101,101 INCLUSIVEB,3043@@101
    10102,102 INCLUSIVEC,3044@@102
    10103,1031 INCLUSIVEBV,3044@@1031

    contragent.csv
    ID_CONTRAGENT,CONTRAGENT_NAME,CONTRAGENT_BULSTAT,CONTRAGENT_VATNUMBER,CONTRAGENT_POST_ADDRESS,CONTRA GENT_Z
    103000001,conragent,0,0,,,,,,,,,,,,
    103000002,BEVERAGESAD,831607255,B831607255,,,paris,,67,,,,,,30,
    103000003,amrel,831430207,,,,berlin,,126,,,,,,

    file.txt
    OPDDS@@1002304974#2/15/2012#201##987#2/16/2012#fakt######10145@@3044@@102
    @@40111@@103000002$BEVERAGESAD$831607255$BG831607255#1002304974#2/15/2012#2/16/2012#872.33#0#0#0#0#0#0@@

    PHP Code:

    $lines 
    file('ena_purchase_invoice.csv');
    $lines2 file('ena_delivery_head.csv');
    $lines3 file('ena_invent.csv');
    $lines4 file('ena_contragent.csv');
    $c_lines count($lines);
    $c_lines2 count($lines2);
    $c_lines3 count($lines3);
    $c_lines4 count($lines4);
    $i 1;
    for(
    $i=1;$i<$c_lines;$i++) {
        
    $array1 explode(","$lines[$i]);
        
    $array2 explode(","$lines2[$i]);
        
    $array3 explode(","$lines3[$i]);
        
    $array4 explode(","$lines4[$i]);
        
    $new_line "ОПДДС@@" $array1[3]  . "#" $array1[4] . "#201##987" ."#" .$array2[2] ."#" $array1[1
        . 
    "#####" $array1[11] . "#" $array2[5]  . "@@" $array3[2]. "@@" 40111 "@@" $array4[0]
        . 
    "$" $array4[1] . "$" $array4[2] . "$" $array4[3] . "#" $array1[3] . "#" $array1[4
        . 
    "#" .$array2[2]. "#" .$array1[5] ."#0#0#0#0#0#0@@" ;
        
    $data $new_line "\n";
    }
    for(
    $i=1;$i<$c_lines;$i++) {
    $array11 explode(","$lines[$i++]);
    $new_line11 "ОПДДС@@" $array11[3]  . "#" $array11[4] . "#201##987" ."#" .$array2[2] ."#" $array11[1
        . 
    "#####" $array11[11] . "#" $array2[5]  . "@@" $array3[2]. "@@" 40111 "@@" $array4[0]
        . 
    "$" $array4[1] . "$" $array4[2] . "$" $array4[3] . "#" $array11[3] . "#" $array11[4
        . 
    "#" .$array2[2]. "#" .$array11[5] ."#0#0#0#0#0#0@@" ;
        
    $data11 $new_line11 "\n";
        }
    echo 
    nl2br($data);
    echo 
    nl2br($data11);
    $File "file.txt"
     
    $Handle fopen($File'w');
     
    $Data $data;
     
    fwrite($Handle$Data);
      print 
    "Data Written"
     
    fclose($Handle); 
    Last edited by krasi_e_d; 02-27-2012 at 03:54 PM.

  • #9
    New Coder
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK! I' m confused.
    I have this files and from them I want to make file.txt with data taken from files. First important file is fakt.csv. Because from this file I'll have number of lines.
    But when I write the file.txt need to take some data from another files and put in between data in txt.file

    fakt.csv
    ID_PURCHASE_INVOICE,DOC_TYPE_NAME,DOC_EXT_CODE,DOC_NUM,DOC_DATE,WOVAT_AMOUNT,VAT_AMOUNT,TOTAL_AMOUNT ,CONTR

    108826,fakt,1,0000111366,2/15
    /2012,160.08,32.02,192.1,prezis,103565237,BG103565237,paris,,,,,,,,,,,,paid,,,,,,,,,,

    108830,fakt,1,1002304974,2/15/2012,872.33,174.46,1046.79,ovm,121759222,BG121759222,,,hamburg,,,,,,,,,,paid,,,,,,,,,,

    delivery_head.csv
    ID_DOC,DOC_NUM,DOC_DATE,ID_CONTRAGENT,ID_PURCHASE_INVOICE,ID_INVENT,ID_OPERATION,IS_ARCHIVE
    1016853,11281,2/16/2012,103000090,108826,10133,1,0
    1016858,11286,2/16/2012,103000221,108830,10145,1,0

    invent.csv
    ID_INVENT,INVENT_NAME,ACCOUNT_TAG
    10101,101 INCLUSIVEB,3043@@101
    10102,102 INCLUSIVEC,3044@@102
    10103,1031 INCLUSIVEBV,3044@@1031

    contragent.csv
    ID_CONTRAGENT,CONTRAGENT_NAME,CONTRAGENT_BULSTAT,CONTRAGENT_VATNUMBER,CONTRAGENT_POST_ADDRESS,CONTRA GENT_Z
    103000001,conragent,0,0,,,,,,,,,,,,
    103000002,BEVERAGESAD,831607255,B831607255,,,paris,,67,,,,,,30,
    103000003,amrel,831430207,,,,berlin,,126,,,,,,

    file.txt
    OPDDS@@1002304974#2/15/2012#201##987#2/16/2012#fakt######10145@@3044@@102
    @@40111@@103000002$BEVERAGESAD$831607255$BG831607255#1002304974#2/15/2012#2/16/2012#872.33#0#0#0#0#0#0@@
    Last edited by krasi_e_d; 02-28-2012 at 08:19 AM.

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    does each file contain the same number of lines?
    how is that data from each file related to the data in the other files?
    which file is the "key" file?

  • #11
    New Coder
    Join Date
    Feb 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by litebearer View Post
    does each file contain the same number of lines?
    how is that data from each file related to the data in the other files?
    which file is the "key" file?
    fakt.csv is the "key" file


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •