简体   繁体   中英

How do I merge lines from a file I import in Python?

I have a file (nameOfTheFile.txt) containing the following data :

-200.0000   23.0786    0.2402    0.9807    2.7610    0.7627    0.3168    1.4249
    0.8745    0.4953    1.4652    5.9483    0.0000    0.6919    2.2648    0.3407
    0.0000    0.6958    0.5775    0.0000    0.6171    2.6211
 -199.9800   23.0706    0.2401    0.9804    2.7598    0.7632    0.3167    1.4246
    0.8743    0.4952    1.4646    5.9452    0.0000    0.6917    2.2638    0.3407
    0.0000    0.6955    0.5774    0.0000    0.6170    2.6203

I import the data in an array that I would like to use later. However, when I do the following

with open(os.path.join(sys.path[0], 'nameOfTheFile.txt'), 'r') as file:
    lines = file.readlines()
    tab = []
    for i in range(len(lines)):
            tab.append(lines[i])
    print(tab)

I get this

['-200.0000   23.0786    0.2402    0.9807    2.7610    0.7627    0.3168    1.4249\n', '    0.8745    0.4953    1.4652    5.9483    0.0000    0.6919    2.2648    0.3407\n', '    0.0000    0.6958    0.5775    0.0000    0.6171    2.6211\n', ' -199.9800   23.0706    0.2401    0.9804    2.7598    0.7632    0.3167    1.4246\n', '    0.8743    0.4952    1.4646    5.9452    0.0000    0.6917    2.2638    0.3407\n', '    0.0000    0.6955    0.5774    0.0000    0.6170    2.6203\n']

I know how to get rid of the \\n but when I do so, I still get this output :

['-200.0000   23.0786    0.2402    0.9807    2.7610    0.7627    0.3168    1.4249', '    0.8745    0.4953    1.4652    5.9483    0.0000    0.6919    2.2648    0.3407', '    0.0000    0.6958    0.5775    0.0000    0.6171    2.6211', ' -199.9800   23.0706    0.2401    0.9804    2.7598    0.7632    0.3167    1.4246', '    0.8743    0.4952    1.4646    5.9452    0.0000    0.6917    2.2638    0.3407', '    0.0000    0.6955    0.5774    0.0000    0.6170    2.6203']

There is still a separation between two lines by a coma, making each line of nameOfTheFile.txt an item of the array. Therefore, when I want to print :

print(tab[0],tab[1])

I obtain

-200.0000   23.0786    0.2402    0.9807    2.7610    0.7627    0.3168    1.4249     0.8745    0.4953    1.4652    5.9483    0.0000    0.6919    2.2648    0.3407

What I would like is that not each line of nameOfTheFile.txt is considered as an item but each values, which would give me by applying the print before :

-200.0000   23.0786

Is there a way to do so please ?

Thanks !

Your file consists of lines of text, but you want numbers. You have to do that conversion yourself.

tab = []
with open(os.path.join(sys.path[0], 'nameOfTheFile.txt'), 'r') as file:
    for line in file:
            tab += [float(n) for n in line.strip().split()]
print(tab)

Looks like you need list.extend

Ex:

tab = []
with open(os.path.join(sys.path[0], 'nameOfTheFile.txt')) as infile:
    for line in infile:
        tab.extend(line.strip().split())  #Strip newline char and split by space. 
print(tab[0],tab[1])

Output:

-200.0000 23.0786

Just read the entire file and split its content:

with open('nameOfTheFile.txt', 'r') as file:
    text = file.read()
    tab = text.split()

print(tab)
print(tab[0], tab[1])

Or use

tab = [float(i) for i in text.split()]

if you need numbers instead of strings.

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