Re: [Tutor] how to join two text files ?
If you need read files like Example file_1 and file_2 you could use csv reader, look this code and think how you could use it: import csv f1 = csv.reader(open('file1', newline=''),delimiter=' ') for a, b, c in f1: print('%s %s %s' % (a, b, c)) 2011/3/29 Peter Otten <__pete...@web.de> > Mateusz K wrote: > > > I have many text files, where data is delimited by space. > > Each file contain three colums: coordinate x, coordinate y and value for > > this location. > > > > I would like to join this data to get one big file which will > > contain columns: > > coordinate x, coordinate y, value1,value2,..., value_n and save it to > > another text file. > > > > I wrote script, but there's some stupid error (like "\n" character > > although I have added .rstrip() command). > > > > Coul You tell me what is most convenient way to join this data > > (maybe by using csv module?)? > > Please show us the code that you have. This proves that you've put some > effort into the matter and allows us to focus on the missing parts. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to join two text files ?
Mateusz K wrote: > I have many text files, where data is delimited by space. > Each file contain three colums: coordinate x, coordinate y and value for > this location. > > I would like to join this data to get one big file which will > contain columns: > coordinate x, coordinate y, value1,value2,..., value_n and save it to > another text file. > > I wrote script, but there's some stupid error (like "\n" character > although I have added .rstrip() command). > > Coul You tell me what is most convenient way to join this data > (maybe by using csv module?)? Please show us the code that you have. This proves that you've put some effort into the matter and allows us to focus on the missing parts. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to join two text files ?
Well...it looks like I do not know how to use it. Could You help me Example file_1: 20 53 2.66196 21 53 2.67512 20 52 2.63444 21 52 2.94148 Example file_2: 20 53 1.75904 21 53 2.92742 20 52 2.79653 21 52 2.12499 and so on my script: import glob {some code here} here is loop which change folder_daty, zmienna and folder_serw:## plik2=folder_daty+zmienna+"XYZ.txt" zbiorcze = folder_serw + zmienna + ".txt" if not os.path.isfile(zbiorcze): shutil.copy(plik2, zbiorcze) else: with open(zbiorcze, "wb") as w: writer = csv.writer(w) for f in glob.glob(plik2): rows = open(f, "rb").readlines() writer.writerows(rows) and result looks like: 2,0, ,5,3, ,2,.,4,4,8,2," " 2,1, ,5,3, ,3,.,0,4,9,9,6," " 2,0, ,5,2, ,3,.,1,8,4,9,5," " 2,1, ,5,2, ,3,.,3,2,2,6,5," " Hello, If the files are not too big, you could do something like: with open("/home/me/Desktop/test.csv", "wb") as w: writer = csv.writer(w) for f in glob.glob("/home/me/Desktop/files/*.txt"): rows = open(f, "rb").readlines() writer.writerows(rows) Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ -------------------- *From:* Mateusz K *To:* tutor@python.org *Sent:* Sat, March 26, 2011 10:08:43 PM *Subject:* [Tutor] how to join two text files ? Hello, I have many text files, where data is delimited by space. Each file contain three colums: coordinate x, coordinate y and value for this location. I would like to join this data to get one big file which will contain columns: coordinate x, coordinate y, value1,value2,..., value_n and save it to another text file. I wrote script, but there's some stupid error (like "\n" character although I have added .rstrip() command). Coul You tell me what is most convenient way to join this data (maybe by using csv module?)? = Best regards, Mateusz ___ Tutor maillist - Tutor@python.org <mailto:Tutor@python.org> To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- *Pozdrawiam / Best regards, * Mateusz Ke;dzior | environmental engineer Uz.ywam "otwartych" <http://pl.wikipedia.org/wiki/OpenDocument> formatów plików biurowych environmental protection: please consider the environment before printing my email ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to join two text files ?
Hello, If the files are not too big, you could do something like: with open("/home/me/Desktop/test.csv", "wb") as w: writer = csv.writer(w) for f in glob.glob("/home/me/Desktop/files/*.txt"): rows = open(f, "rb").readlines() writer.writerows(rows) Cheers!! Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ From: Mateusz K To: tutor@python.org Sent: Sat, March 26, 2011 10:08:43 PM Subject: [Tutor] how to join two text files ? Hello, I have many text files, where data is delimited by space. Each file contain three colums: coordinate x, coordinate y and value for this location. I would like to join this data to get one big file which will contain columns: coordinate x, coordinate y, value1,value2,..., value_n and save it to another text file. I wrote script, but there's some stupid error (like "\n" character although I have added .rstrip() command). Coul You tell me what is most convenient way to join this data (maybe by using csv module?)? = Best regards, Mateusz ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to join two text files ?
On 26/03/2011 21:08, Mateusz K wrote: Hello, I have many text files, where data is delimited by space. Each file contain three colums: coordinate x, coordinate y and value for this location. I would like to join this data to get one big file which will contain columns: coordinate x, coordinate y, value1,value2,..., value_n and save it to another text file. I wrote script, but there's some stupid error (like "\n" character although I have added .rstrip() command). Coul You tell me what is most convenient way to join this data (maybe by using csv module?)? = Best regards, Mateusz ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Maybe use split like this, 'x y 123.456\n'.split() ['x', 'y', '123.456'] then store the x and y coords in a dict with a list of values, perhaps sort according to your needs, write output to file. HTH. Mark L. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to join two text files ?
Hello, I have many text files, where data is delimited by space. Each file contain three colums: coordinate x, coordinate y and value for this location. I would like to join this data to get one big file which will contain columns: coordinate x, coordinate y, value1,value2,..., value_n and save it to another text file. I wrote script, but there's some stupid error (like "\n" character although I have added .rstrip() command). Coul You tell me what is most convenient way to join this data (maybe by using csv module?)? = Best regards, Mateusz ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor