Note that there are some explanatory texts on larger screens.

plurals
  1. PORead file and get certain value from each line of file
    primarykey
    data
    text
    <p>I'm stuck on a particular point on something, I'm hoping you guys could perhaps suggest a better method.</p> <p>For each line of file I'm reading I want to get the nth word of the line, store this and print them on a single line.</p> <p>I have the following code:</p> <pre><code>import os p = './output.txt' word_line = ' ' myfile = open(p, 'r') for words in myfile.readlines()[1:]: # I remove the first line because I don't want it current_word = words.strip().split(' ')[4] word_line += current_word print word_line myfile.close() </code></pre> <p>The file it reads looks like this:</p> <pre><code>1 abc-abc.abc (1235456) [AS100] bla 123 etc 2 abc-abc.abc (1235456) [AS10] bla 123 etc 3 abc-abc.abc (1235456) [AS1] bla 123 etc 4 abc-abc.abc (1235456) [AS56] bla 123 etc 5 abc-abc.abc (1235456) [AS8] bla 123 etc 6 abc-abc.abc (1235456) [AS200] bla 123 etc etc </code></pre> <p>My current code outputs the following:</p> <pre><code>[AS100][AS10][AS1][AS56][AS8][AS200] </code></pre> <p>Only problem is, it is not always fixed as the 4th value of the line, as sometimes it appears as 5th, etc or not at all.</p> <p>I'm currently trying out:</p> <pre><code>if re.match("[AS", words): f_word = re.match(".*[(.*)",words) </code></pre> <p>This isn't working out, I'm trying to see if in the current line it finds an open "[" If it does to display the content of it before the closing "]. Move on to the new line and keep on doing this.</p> <p>Eventually have the following desired output:</p> <pre><code>AS100 AS10 AS1 AS56 AS8 AS200 </code></pre> <p>I could really use some advise on this. Thanks</p> <p>EDIT:</p> <pre><code>m = re.search(r'\[AS(.*?)]', words) if m: f_word += ' ' + m.group(1) </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload