Bando
Bando

Reputation: 1302

How can I grab some values from a text file into a list then write it?

I'm setting up a script and I need to grab some values from a text file into a list. Here is the architecture of my text file:

someValue
someValue
someValue
Value
 example1
 example2
Value
 example3
 example4
someValue
someValue
Value
 example5
[...]

The expected output is:

my_list = [['Value', 'example1', 'example2', '\n'], ['Value', 'example3', 'example4', '\n'], ['example5', ..]]

But I obtain this:

my_list = [['Value', 'example1', 'example2'], ['Value', 'example1', 'example2'], ['Value', 'example1', ..]]

and when I'm trying to write it on a file, I wrote this:

[example1, example2] on my file.

but I want to obtain this (with the '\n'):

example1
example2

I've tried this:

f = open(file, 'r')
for num, lines in enumerate(f, 1):
   my_list.append(lines)
   if 'Value' in lines:
      my_list_2.append(num)

for i in range(len(my_list_2)):
     number_of_lines = my_list_2[i+1] - my_list_2[i]
     for j in range(int(number_of_lines)):
          extract.append(my_list[my_list_2[0]+j])
     file = open(file2, 'w')
     for k in range(len(extract)):
         file.write(extract[k])

Every kind of help is appreciated. Thanks in advance.

Upvotes: 0

Views: 64

Answers (2)

Bando
Bando

Reputation: 1302

I will try to explain how I pass through this issue:

for num, lines in enumerate(f, 1):
    my_list.append(lines)

newlist = []
for i in range(len(my_list)):
    splitlist = my_list[i].splitlines()

    for j in range(len(splitlist)):
        newlist.append(splitlist[j])

By doing that, I've obtained a list which elements are all the lines from my file.

Then I've created some lists which contains the indices of the apparition of my specific string as:

index = []
for i in range(len(newlist)):
    if newlist[i].startswith('string1'):
         index.append(i+1)

I pass through the issue of adding \n because I just manage it when I wrote all the items of my list in my new text file.

Hope I've explained it well. For any kind of question, just comment.

Upvotes: 0

Paragalor
Paragalor

Reputation: 71

Consider an approach that captures relevant lines on the first read. We can set a boolean to let the loop know whether we should be adding lines whenever we come across Value:

f = open(file, 'r')
lines = f.readlines()
# what we'll be writing to a file
output = list()
# the current captured lines to be added to output
current = list()
# boolean specifying whether we should be trying to add lines to current
found = False

for line in lines:
    # stop adding lines to current when we encounter a line without a space as its first character
    if found and not line.startswith(' '):
        found = False
        output.append(list(current))
        current = list()

    # add lines to our current list if our boolean is set, otherwise be looking for 'Value'
    if found:
        current.append(line[1:])
    elif line == 'Value\n':
        found = True
        current.append(line)

# make sure to add values if current isn't empty after the loop's execution
if current:
    output.append(current)

This gives us our output:

output = [['Value\n', 'example1\n', 'example2\n'], ['Value\n', 'example3\n', 'example4\n'], ['Value\n', 'example5\n']]

Which we can then easily write to a file (make sure you open with the append option a):

with open(file2, 'a') as wf:
    for x in output:
        for val in x[1:]:
            wf.write(val)

The output file's contents will be:

example1
example2
example3
example4
example5

Including a trailing newline. Hope this helps!

Upvotes: 1

Related Questions