简体   繁体   中英

How remove the line breaks with a tuple in python

I want to join four files in one without the line breaks. But when I used line.strip()

arquivos=open("tst-temp.dat","r")
arquivo=open("cvt-temp.dat","r")
arq=open("cvtzct-temp.dat", "r")
ar=open("cvtsct-temp.dat", "r")
orig_stdout = sys.stdout
f = open("total-anar.dat","w")
sys.stdout = f
for line in zip[arquivos,arquivo,arq,ar]: 
    print(line.strip())
f.close()

Python returns the following error:

AttributeError: 'tuple' object has no attribute 'strip'

If I remove line.strip(), my exiting file looks like this:

('#     T       TST\n', '     CVT\n', '    CVT/ZC\n', '     CVT/S\n')
('--------------------\n', '----------\n', '----------\n', '----------\n')
('100       7.30e-20\n', '7.29e-20\n', '2.17e-17\n', '6.11e-16\n')
('200       9.62e-16\n', '9.61e-16\n', '2.90e-15\n', '1.15e-14\n')
('300       2.59e-14\n', '2.59e-14\n', '4.11e-14\n', '8.09e-14\n')
('400       1.53e-13\n', '1.52e-13\n', '1.99e-13\n', '2.97e-13\n')
('500       4.94e-13\n', '4.87e-13\n', '5.83e-13\n', '7.63e-13\n')
('600       1.16e-12\n', '1.14e-12\n', '1.30e-12\n', '1.59e-12\n')
('700       2.29e-12\n', '2.23e-12\n', '2.47e-12\n', '2.87e-12\n')
('800       3.99e-12\n', '3.86e-12\n', '4.19e-12\n', '4.72e-12\n')
('900       6.38e-12\n', '6.14e-12\n', '6.56e-12\n', '7.23e-12\n')
('1000      9.59e-12\n', '9.16e-12\n', '9.69e-12\n', '1.05e-11\n')
('1100      1.37e-11\n', '1.30e-11\n', '1.37e-11\n', '1.46e-11\n')
('1200      1.89e-11\n', '1.76e-11\n', '1.82e-11\n', '1.92e-11\n')
('1300      2.53e-11\n', '2.34e-11\n', '2.39e-11\n', '2.52e-11\n')
('1400      3.30e-11\n', '3.02e-11\n', '3.08e-11\n', '3.22e-11\n')
('1500      4.19e-11\n', '3.81e-11\n', '3.87e-11\n', '4.02e-11\n')
('1600      5.25e-11\n', '4.73e-11\n', '4.72e-11\n', '4.89e-11\n')
('1700      6.46e-11\n', '5.76e-11\n', '5.74e-11\n', '5.92e-11\n')
('1800      7.82e-11\n', '6.91e-11\n', '6.81e-11\n', '7.00e-11\n')
('1900      9.36e-11\n', '8.18e-11\n', '8.05e-11\n', '8.25e-11\n')
('2000      1.11e-10\n', '9.58e-11\n', '9.42e-11\n', '9.64e-11\n')
('2500      2.24e-10\n', '1.86e-10\n', '1.83e-10\n', '1.86e-10\n')

I need the exit file similar to:

#T (K)          TST        CVT    CVT/ZCT    CVT/SCT

 100.00  7.899E-40  1.400E-40  4.342E-34  1.902E-32
 200.00  1.098E-25  5.007E-26  6.373E-25  2.474E-24
 300.00  6.550E-21  4.157E-21  1.067E-20  1.867E-20
 400.00  1.767E-18  1.327E-18  2.139E-18  2.891E-18
 500.00  5.505E-17  4.555E-17  5.899E-17  7.120E-17
 600.00  5.818E-16  5.083E-16  5.817E-16  6.614E-16
 700.00  3.302E-15  2.977E-15  3.172E-15  3.482E-15
 800.00  1.265E-14  1.161E-14  1.183E-14  1.270E-14
 900.00  3.715E-14  3.442E-14  3.410E-14  3.604E-14
1000.00  9.024E-14  8.403E-14  8.170E-14  8.541E-14
1100.00  1.904E-13  1.777E-13  1.707E-13  1.770E-13
1200.00  3.609E-13  3.361E-13  2.855E-13  2.942E-13
1300.00  6.285E-13  5.814E-13  4.971E-13  5.100E-13
1400.00  1.023E-12  9.409E-13  8.098E-13  8.278E-13
1500.00  1.576E-12  1.442E-12  1.249E-12  1.273E-12
1600.00  2.319E-12  2.113E-12  1.840E-12  1.871E-12
1700.00  3.286E-12  2.982E-12  2.610E-12  2.649E-12
1800.00  4.507E-12  4.076E-12  3.586E-12  3.633E-12
1900.00  6.012E-12  5.422E-12  4.791E-12  4.848E-12
2000.00  7.831E-12  7.043E-12  6.252E-12  6.318E-12
2500.00  2.251E-11  2.004E-11  1.812E-11  1.824E-11

How can I remove the line breaks?

Try iterating over the elements and constructing a new tuple from the modified values.

# Assuming our tuple is called "line"

tuple(element.strip() for element in line)

Tuples in python are immutable. In other words, if you want to change the values in them, then you have to create a new one.

To combine with your code:

arquivos=open("tst-temp.dat","r")
arquivo=open("cvt-temp.dat","r")
arq=open("cvtzct-temp.dat", "r")
ar=open("cvtsct-temp.dat", "r")
orig_stdout = sys.stdout
f = open("total-anar.dat","w")
sys.stdout = f
for line in zip[arquivos,arquivo,arq,ar]: 
    print(tuple(element.strip() for element in line))
f.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM