...

View Full Version : Convert csv to txt?



krasi_e_d
02-23-2012, 09:38 PM
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?

themousemaster
02-23-2012, 09:48 PM
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


get_line_of_data_from_csv;
if(conditions_for_each_line){
write to txt file;
}

krasi_e_d
02-24-2012, 06:52 AM
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


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.

krasi_e_d
02-26-2012, 11:27 AM
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:


$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.

litebearer
02-26-2012, 12:28 PM
Here is a start - I'll let you finish it...

<?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);
?>

krasi_e_d
02-27-2012, 07:14 AM
Here is a start - I'll let you finish it...

<?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.

litebearer
02-27-2012, 12:07 PM
show your most recent code

krasi_e_d
02-27-2012, 12:12 PM
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@@




$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);

krasi_e_d
02-28-2012, 07:17 AM
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@@

litebearer
02-28-2012, 11:57 AM
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?

krasi_e_d
02-29-2012, 06:02 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum