Open file with Python. Question

Asked 2 years ago, Updated 2 years ago, 14 views

Enter ping to CMD and save the value as txt Open the txt file with Python and open After finding a specific value using the find in the txt file, I'm going to compare the first and second values, but if you read lines and then line[1], the first line comes out When you enter range, line[1], you get only one second character in the first line, not the first line.

How do I get a value that corresponds to the line in the txt file while turning it to range? (If you do line[0], the first line comes out instead of the first letter)

Below is the code

for x in range(len(line)):

            idx = line[x].find("[")
            if idx == 0:
                line[x]=line[x].replace('[{','')
                line[x]=line[x].replace('}]','')
                line[x]=line[x].replace('"','')
                spl = line[x].split(',')
                data1 = 0               
                for y in range(len(spl)):
                    dat = spl[y].find(spl[12]) 
                    times = spl[y].find(spl[2])
                    if times == 0:
                        tim = spl[y].split(':')
                    if dat == 0:
                        dat = spl[y].split(':')
                        data1 = dat[1]

python

2022-09-22 08:27

2 Answers

Well...

When one line of values in the array appears, it seems like you did fileObject.readlines(), and

I think you did fileObject.readline() when only one character appears in the array.

(Difference between s and at the end of the method)

Check it out.


2022-09-22 08:27

First of all, you can use the enumerate function to obtain the line number as shown below.

The problem with the parsing part should be the source string and the description of the purpose part.

with open('file path') as f:
    for i, v in enumerate(f):
        print(i, v)


2022-09-22 08:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.