creating two separate lists read from a file in python
<p>Can i please know how the data from a file can be split into two separate lists. For example,
file contains data as 1,2,3,4;5,6,7 </p>

<pre><code>my code:

for num in open('filename','r'):
  list1 = num.strip(';').split()
</code></pre>

<p>Here , i want a new list before semi colon (i.e) [1,2,3,4] and new list after semi colon (i.e) [5,6,7]</p>

<p>Depending on the size of your file, you could simply read the whole file into a string at once and then first split by semicolon, then by comma:</p>

<pre class="lang-python prettyprint-override"><code>with open('filename', 'r') as f:  #open file
    s = f.read()   #read entire contents into string
    lists = s.split(';')  #split separate lists by semicolon delimiters
    for l in lists:  #for each list
        l = [int(x) for x in l.split(',')]  #separate the string by commas and convert to integers
</code></pre>

python python-2.7 python-3.x