简体   繁体   中英

Extracting data from a .txt file

I have this data in my sample.txt file:

A2B3,32:45:63
A4N6,17:72:35
S2R3,13:14:99

What I want to do is to put those data in an array but I'm having problems separating those with commas.

My code goes like this:

with open('sample.txt', 'r') as f:
    for line in f:
        x = f.read().splitlines()
        print(x)

And the output goes like this:

['A2B3,32:45:63','A4N6,17:72:35','S2R3,13:14:99']

I altered my code in different ways to separate those two variables with commas but I can't seem to make it work. Can someone help me achieve this output?

['A2B3','32:45:63','A4N6','17:72:35','S2R3','13:14:99']

use line.split(',') to seperate the line at the ",".

x = []
with open('sample.txt', 'r') as f:
    for line in f:
        for j in line.split(','):
            x.append(j.split('\n')[0])
    print(x)

You probably could just do:

data = list()
with open('sample.txt', 'r') as f:
for line in f.readlines():
    data.append(line)

And you should end up with list of appended lines. It's also faster on big files than just .splitlines() since .readlines() is implemented in C and doesn't load whole file in memory.

Use this code, which splits the lines into a list like you have, and then splits those items at the comma.

filename = "sample.txt"

with open(filename) as file:
    lines = file.read().split("\n")

output = []
for l in lines:
    for j in l.split(","):
        output.append(j)

print(output)

Output:

['A2B3', '32:45:63', 'A4N6', '17:72:35', 'S2R3', '13:14:99']

yes, it's very simple...

after separate all line, you get list look like

['A2B3,32:45:63','A4N6,17:72:35','S2R3,13:14:99']

then after again you separate that each element by comma(,) and add it into new list like

list_a = ['A2B3,32:45:63','A4N6,17:72:35','S2R3,13:14:99']
final_list = []

for i in list_a:
    part_1, part_2 = i.split(',')
    final_list.append(part_1)
    final_list.append(part_2)

print(final_list)

And it will give your desire output like

['A2B3','32:45:63','A4N6','17:72:35','S2R3','13:14:99']

it is not a redundant way but for you very easy to understand

Thank You :)

Here you go, just iterating once over the lines:

res = []
with open('sample.txt', 'r') as f:
    for line in f:
        res += line.strip().split(",")
print(res)

Gives:

['A2B3', '32:45:63', 'A4N6', '17:72:35', 'S2R3', '13:14:99']

Though I wonder why you'd want to have everything in a list, I think you are missing the link between the items, maybe could be more useful to keep them in tuples like this:

res = []
with open('sample.txt', 'r') as f:
    for line in f:
        res.append(tuple(line.strip().split(",")))
print(res)

Gives:

[('A2B3', '32:45:63'), ('A4N6', '17:72:35'), ('S2R3', '13:14:99')]

FMPOV this result is better to go along. But nevermind, I guess, you'll find your solution from one of those poseted here.

x = [i.replace("\n","").split(',')for i in open('data.txt', 'r')]
print(x)
print(x[0][1])

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