0

I have two list of almost 100 float numbers values each and I need to save their data into one txt file with two columns. But, I need this file to reopen it and adding extra Data and save it, at least 3 times in my code... please if someone have any idea ...

user1640255
  • 1,224
  • 3
  • 19
  • 25
  • 1
    Which part do you have problems with and what have you tried so far? – Burhan Khalid Sep 23 '12 at 07:45
  • Do you know that you can [open files in append mode](http://stackoverflow.com/questions/4706499/how-do-you-append-to-file-in-python)? – Tim Pietzcker Sep 23 '12 at 07:47
  • yes I open it = open('file', 'w') .. after that I cant understand how to write data of two lists, side by side, to a txt file and reopen add more data... logically i will use append, but how to save ? – user1640255 Sep 23 '12 at 07:52
  • I tried savetxt(), csv.writer() snd pickle() but I can't manage to save 2 lists float data to one txt file side by side... – user1640255 Sep 23 '12 at 07:55

2 Answers2

1

You can try this idea:

First, write two lists into a two column text file.

a=[0.2342,2.0002,0.1901]
b=[0.4245,0.5123,6.1002] 
c = [a, b] 
with open("list1.txt", "w") as file:
    for x in zip(*c):
        file.write("{0}\t{1}\n".format(*x))

Second, reopen the saved file list1.txt

with open("list1.txt", "r+") as file:
    d = file.readlines()

Third, Adding extra data

e=[1.2300,0.0002,2.0011]
f=[0.4000,1.1004,0.9802]
g = [e, f]
h = []
for i in d:
    h.append(i)
for x in zip(*g):
    h.append("{0}\t{1}\n".format(*x))

Fourth, save the text file

with open("list2.txt", "w") as file1:
    for x in h:
        file1.writelines(x)

Output in the list2.txt file looks as follows

0.2342  0.4245
2.0002  0.5123
0.1901  6.1002
1.23    0.4
0.0002  1.1004
2.0011  0.9802
Ashok Kumar Jayaraman
  • 2,887
  • 2
  • 32
  • 40
0

It depends on how you want to separate the two columns (with spaces, tabs or commas). Here's how I'd do it the quick 'n' dirty way with a space as the separator:

Python 2:

with open('output.txt', 'w') as f:
    for f1, f2 in zip(A, B):
        print >> f, f1, f2

Python 3:

with open('output.txt', 'w') as f:
    for f1, f2 in zip(A, B):
        print(f1, f2, file=f)
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • .. sorry I don't understand, I have to save first each list with numbers to a file#.txt and afterwards to use your code? – user1640255 Sep 23 '12 at 15:41
  • I have two lists, A[i] and B[i], I can print their "float" data. Just these data I want to save in one file side by side as columns , separated with a space – user1640255 Sep 23 '12 at 15:47
  • error " print >> f, f1, f2 " "unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'" ... why i need this? the only I want is to write into the output.txt file the data of A and B lists... and reopen to add more later – user1640255 Sep 23 '12 at 22:33
  • yes it's wotking!!! thank you!!! ... and if i want to reopen the output.txt file to write extra data? – user1640255 Sep 23 '12 at 22:47
  • and if A and B are not list, then I will use str ... ex. for f1 and f2 in zip(str(C), A, B) ? – user1640255 Sep 23 '12 at 23:04
  • `open('output.txt', 'a')` to add more data to it. If A and B are not lists, you wouldn't need to iterate over them in the first place (just do `print >> f, A, B`) – nneonneo Sep 23 '12 at 23:06
  • Yes...please, these are very basic Python questions. Perhaps you would be served well by taking a tutorial? http://getpython3.com/diveintopython3/, http://docs.python.org/dev/tutorial/ – nneonneo Sep 24 '12 at 00:31
  • I was looking for the most Pythonic way to simulate an awk approach for duplicating columns; i.e. awk '{print $1,$1}' file_in > file_out. Your post inspired me how to do this in an elegant way. Thank you. – tommy.carstensen May 10 '13 at 22:14