StopIteration error before reading the text file using next()












0














I created this code to scan my samples_vsdt.txt getting a certain values then writing it in a csv, I'm having an error StopIteration and doesn't even read the text file. I'm trying to solve this for hours, any idea what's causing the problem?



Here is how my code works, Example this line:



Scanning samples_extracted82e5b144cb5f1c10629e72fc1291f535db7b0b40->(Word 2003 XML Document 1003-1)


Will be written to csv as this:



82e5b144cb5f1c10629e72fc1291f535db7b0b40,Word 2003 XML Document 1003-1


Here is my code, and this is working for all my txt_files but this one sample_vsdt.txt doesn't work properly



import csv,re
out_vsdt = "samples_vsdt.txt"
out_sha1_vsdt = "sha1_vsdt.csv"

def read_text_file(out_vsdt):

with open(out_vsdt) as f:
data =
for line in f:
if "Scanning " + new in line and "(" in line:

try:
sha = re.search('\(.*)->', line).group(1)
desc= re.search('->((.*))', line).group(1)
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)

continue
if "Scanning " + new in line:
try:
sha= re.search('\(.*)$', line).group(1)
while True:
i = next(f)
if "(" in i:
try:
desc = re.search('->((.*))', i).group(1)
break
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)
except AttributeError:
sha = None
return data

def write_csv_file(data,out_sha1_vsdt):
with open(out_sha1_vsdt, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
csvwriter.writerow(['SHA-1','VSDT','DESC'])
for row in data:

csvwriter.writerow(row)

def main():
data = read_text_file(out_vsdt)
write_csv_file(data, out_sha1_vsdt)

if __name__ == '__main__':
main()

print "Parsing Successful"


Gives me error:



Traceback (most recent call last):
File "C:UserstrendMICRODesktopojtscannerparser.py", line 65, in <module>
main()
File "C:UserstrendMICRODesktopojtscannerparser.py", line 61, in main
data = read_text_file(out_vsdt)
File "C:UserstrendMICRODesktopojtscannerparser.py", line 37, in read_text_file
i = next(f)
StopIteration









share|improve this question


















  • 1




    Raising StopIteration is part of the specification of next. It is how end of iteration is signaled. The error is thus to be expected. Your file is likely malformed, missing the break in your while True: i = next(f) loop.
    – MisterMiyagi
    Nov 20 '18 at 7:54






  • 1




    You are iterating ofer the same open file twice, once with the for loop and once with next().
    – Klaus D.
    Nov 20 '18 at 7:55










  • what should I remove here or add?
    – Godshand
    Nov 20 '18 at 8:55
















0














I created this code to scan my samples_vsdt.txt getting a certain values then writing it in a csv, I'm having an error StopIteration and doesn't even read the text file. I'm trying to solve this for hours, any idea what's causing the problem?



Here is how my code works, Example this line:



Scanning samples_extracted82e5b144cb5f1c10629e72fc1291f535db7b0b40->(Word 2003 XML Document 1003-1)


Will be written to csv as this:



82e5b144cb5f1c10629e72fc1291f535db7b0b40,Word 2003 XML Document 1003-1


Here is my code, and this is working for all my txt_files but this one sample_vsdt.txt doesn't work properly



import csv,re
out_vsdt = "samples_vsdt.txt"
out_sha1_vsdt = "sha1_vsdt.csv"

def read_text_file(out_vsdt):

with open(out_vsdt) as f:
data =
for line in f:
if "Scanning " + new in line and "(" in line:

try:
sha = re.search('\(.*)->', line).group(1)
desc= re.search('->((.*))', line).group(1)
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)

continue
if "Scanning " + new in line:
try:
sha= re.search('\(.*)$', line).group(1)
while True:
i = next(f)
if "(" in i:
try:
desc = re.search('->((.*))', i).group(1)
break
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)
except AttributeError:
sha = None
return data

def write_csv_file(data,out_sha1_vsdt):
with open(out_sha1_vsdt, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
csvwriter.writerow(['SHA-1','VSDT','DESC'])
for row in data:

csvwriter.writerow(row)

def main():
data = read_text_file(out_vsdt)
write_csv_file(data, out_sha1_vsdt)

if __name__ == '__main__':
main()

print "Parsing Successful"


Gives me error:



Traceback (most recent call last):
File "C:UserstrendMICRODesktopojtscannerparser.py", line 65, in <module>
main()
File "C:UserstrendMICRODesktopojtscannerparser.py", line 61, in main
data = read_text_file(out_vsdt)
File "C:UserstrendMICRODesktopojtscannerparser.py", line 37, in read_text_file
i = next(f)
StopIteration









share|improve this question


















  • 1




    Raising StopIteration is part of the specification of next. It is how end of iteration is signaled. The error is thus to be expected. Your file is likely malformed, missing the break in your while True: i = next(f) loop.
    – MisterMiyagi
    Nov 20 '18 at 7:54






  • 1




    You are iterating ofer the same open file twice, once with the for loop and once with next().
    – Klaus D.
    Nov 20 '18 at 7:55










  • what should I remove here or add?
    – Godshand
    Nov 20 '18 at 8:55














0












0








0







I created this code to scan my samples_vsdt.txt getting a certain values then writing it in a csv, I'm having an error StopIteration and doesn't even read the text file. I'm trying to solve this for hours, any idea what's causing the problem?



Here is how my code works, Example this line:



Scanning samples_extracted82e5b144cb5f1c10629e72fc1291f535db7b0b40->(Word 2003 XML Document 1003-1)


Will be written to csv as this:



82e5b144cb5f1c10629e72fc1291f535db7b0b40,Word 2003 XML Document 1003-1


Here is my code, and this is working for all my txt_files but this one sample_vsdt.txt doesn't work properly



import csv,re
out_vsdt = "samples_vsdt.txt"
out_sha1_vsdt = "sha1_vsdt.csv"

def read_text_file(out_vsdt):

with open(out_vsdt) as f:
data =
for line in f:
if "Scanning " + new in line and "(" in line:

try:
sha = re.search('\(.*)->', line).group(1)
desc= re.search('->((.*))', line).group(1)
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)

continue
if "Scanning " + new in line:
try:
sha= re.search('\(.*)$', line).group(1)
while True:
i = next(f)
if "(" in i:
try:
desc = re.search('->((.*))', i).group(1)
break
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)
except AttributeError:
sha = None
return data

def write_csv_file(data,out_sha1_vsdt):
with open(out_sha1_vsdt, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
csvwriter.writerow(['SHA-1','VSDT','DESC'])
for row in data:

csvwriter.writerow(row)

def main():
data = read_text_file(out_vsdt)
write_csv_file(data, out_sha1_vsdt)

if __name__ == '__main__':
main()

print "Parsing Successful"


Gives me error:



Traceback (most recent call last):
File "C:UserstrendMICRODesktopojtscannerparser.py", line 65, in <module>
main()
File "C:UserstrendMICRODesktopojtscannerparser.py", line 61, in main
data = read_text_file(out_vsdt)
File "C:UserstrendMICRODesktopojtscannerparser.py", line 37, in read_text_file
i = next(f)
StopIteration









share|improve this question













I created this code to scan my samples_vsdt.txt getting a certain values then writing it in a csv, I'm having an error StopIteration and doesn't even read the text file. I'm trying to solve this for hours, any idea what's causing the problem?



Here is how my code works, Example this line:



Scanning samples_extracted82e5b144cb5f1c10629e72fc1291f535db7b0b40->(Word 2003 XML Document 1003-1)


Will be written to csv as this:



82e5b144cb5f1c10629e72fc1291f535db7b0b40,Word 2003 XML Document 1003-1


Here is my code, and this is working for all my txt_files but this one sample_vsdt.txt doesn't work properly



import csv,re
out_vsdt = "samples_vsdt.txt"
out_sha1_vsdt = "sha1_vsdt.csv"

def read_text_file(out_vsdt):

with open(out_vsdt) as f:
data =
for line in f:
if "Scanning " + new in line and "(" in line:

try:
sha = re.search('\(.*)->', line).group(1)
desc= re.search('->((.*))', line).group(1)
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)

continue
if "Scanning " + new in line:
try:
sha= re.search('\(.*)$', line).group(1)
while True:
i = next(f)
if "(" in i:
try:
desc = re.search('->((.*))', i).group(1)
break
except AttributeError:
desc = None
sha = None
mix = sha,desc
data.append(mix)
except AttributeError:
sha = None
return data

def write_csv_file(data,out_sha1_vsdt):
with open(out_sha1_vsdt, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='"')
csvwriter.writerow(['SHA-1','VSDT','DESC'])
for row in data:

csvwriter.writerow(row)

def main():
data = read_text_file(out_vsdt)
write_csv_file(data, out_sha1_vsdt)

if __name__ == '__main__':
main()

print "Parsing Successful"


Gives me error:



Traceback (most recent call last):
File "C:UserstrendMICRODesktopojtscannerparser.py", line 65, in <module>
main()
File "C:UserstrendMICRODesktopojtscannerparser.py", line 61, in main
data = read_text_file(out_vsdt)
File "C:UserstrendMICRODesktopojtscannerparser.py", line 37, in read_text_file
i = next(f)
StopIteration






python csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 7:50









Godshand

748




748








  • 1




    Raising StopIteration is part of the specification of next. It is how end of iteration is signaled. The error is thus to be expected. Your file is likely malformed, missing the break in your while True: i = next(f) loop.
    – MisterMiyagi
    Nov 20 '18 at 7:54






  • 1




    You are iterating ofer the same open file twice, once with the for loop and once with next().
    – Klaus D.
    Nov 20 '18 at 7:55










  • what should I remove here or add?
    – Godshand
    Nov 20 '18 at 8:55














  • 1




    Raising StopIteration is part of the specification of next. It is how end of iteration is signaled. The error is thus to be expected. Your file is likely malformed, missing the break in your while True: i = next(f) loop.
    – MisterMiyagi
    Nov 20 '18 at 7:54






  • 1




    You are iterating ofer the same open file twice, once with the for loop and once with next().
    – Klaus D.
    Nov 20 '18 at 7:55










  • what should I remove here or add?
    – Godshand
    Nov 20 '18 at 8:55








1




1




Raising StopIteration is part of the specification of next. It is how end of iteration is signaled. The error is thus to be expected. Your file is likely malformed, missing the break in your while True: i = next(f) loop.
– MisterMiyagi
Nov 20 '18 at 7:54




Raising StopIteration is part of the specification of next. It is how end of iteration is signaled. The error is thus to be expected. Your file is likely malformed, missing the break in your while True: i = next(f) loop.
– MisterMiyagi
Nov 20 '18 at 7:54




1




1




You are iterating ofer the same open file twice, once with the for loop and once with next().
– Klaus D.
Nov 20 '18 at 7:55




You are iterating ofer the same open file twice, once with the for loop and once with next().
– Klaus D.
Nov 20 '18 at 7:55












what should I remove here or add?
– Godshand
Nov 20 '18 at 8:55




what should I remove here or add?
– Godshand
Nov 20 '18 at 8:55












1 Answer
1






active

oldest

votes


















0














An alternative approach could be to just use a regular expression to extract whole blocks:



import csv
import re

out_vsdt = "samples_vsdt.txt"
out_sha1_vsdt = "sha1_vsdt.csv"

with open(out_vsdt) as f_input:
vscan32 = f_input.read()

with open(out_sha1_vsdt, 'w', newline='') as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(['SHA-1', 'VSDT', 'DESC'])

for sha, desc, vsdt in re.findall(r'Scanning.*?\([0-9a-f]+)(.*?)->((.*?))$', vscan32, re.S + re.M):
desc = '|'.join(line.strip() for line in desc.splitlines() if len(line.strip()))
desc = ''.join(filter(lambda x: x in string.printable, desc)) # remove non-printable characters
csv_output.writerow([sha, vsdt, desc])


This uses a multi-line expression that looks for blocks starting with Scanning. Where there are multiple lines, the lines are stripped and joined together using a |. Finally any non-printable characters are removed from the description.



This would give you an output starting something like:



SHA-1,VSDT,DESC
004d44eeecae27314f8bd3825eb82d2f40182b51,WIN32 EXE 7-2,
07eab9ea58d4669febf001d52c5182ecf579c407,WIN32 EXE 7-2,
0d558bb5e0a5b544621af0ffde1940615ac39deb,WIN32 EXE 7-2,
5172c70c1977bbddc2a163f6ede46595109c7835,WIN32 EXE 7-2,- $R0NsCpuCNMiner32.exe->Found Virus [WORM_CO.331300D2]|- $R0NsCpuCNMiner64.exe->Found Virus [WORM_CO.331300D2]|- $R0NsGpuCNMiner.exe->Found Virus [TROJ64_.743CC567]


This assumes you are using Python 3.x






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388430%2fstopiteration-error-before-reading-the-text-file-using-next%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    An alternative approach could be to just use a regular expression to extract whole blocks:



    import csv
    import re

    out_vsdt = "samples_vsdt.txt"
    out_sha1_vsdt = "sha1_vsdt.csv"

    with open(out_vsdt) as f_input:
    vscan32 = f_input.read()

    with open(out_sha1_vsdt, 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(['SHA-1', 'VSDT', 'DESC'])

    for sha, desc, vsdt in re.findall(r'Scanning.*?\([0-9a-f]+)(.*?)->((.*?))$', vscan32, re.S + re.M):
    desc = '|'.join(line.strip() for line in desc.splitlines() if len(line.strip()))
    desc = ''.join(filter(lambda x: x in string.printable, desc)) # remove non-printable characters
    csv_output.writerow([sha, vsdt, desc])


    This uses a multi-line expression that looks for blocks starting with Scanning. Where there are multiple lines, the lines are stripped and joined together using a |. Finally any non-printable characters are removed from the description.



    This would give you an output starting something like:



    SHA-1,VSDT,DESC
    004d44eeecae27314f8bd3825eb82d2f40182b51,WIN32 EXE 7-2,
    07eab9ea58d4669febf001d52c5182ecf579c407,WIN32 EXE 7-2,
    0d558bb5e0a5b544621af0ffde1940615ac39deb,WIN32 EXE 7-2,
    5172c70c1977bbddc2a163f6ede46595109c7835,WIN32 EXE 7-2,- $R0NsCpuCNMiner32.exe->Found Virus [WORM_CO.331300D2]|- $R0NsCpuCNMiner64.exe->Found Virus [WORM_CO.331300D2]|- $R0NsGpuCNMiner.exe->Found Virus [TROJ64_.743CC567]


    This assumes you are using Python 3.x






    share|improve this answer


























      0














      An alternative approach could be to just use a regular expression to extract whole blocks:



      import csv
      import re

      out_vsdt = "samples_vsdt.txt"
      out_sha1_vsdt = "sha1_vsdt.csv"

      with open(out_vsdt) as f_input:
      vscan32 = f_input.read()

      with open(out_sha1_vsdt, 'w', newline='') as f_output:
      csv_output = csv.writer(f_output)
      csv_output.writerow(['SHA-1', 'VSDT', 'DESC'])

      for sha, desc, vsdt in re.findall(r'Scanning.*?\([0-9a-f]+)(.*?)->((.*?))$', vscan32, re.S + re.M):
      desc = '|'.join(line.strip() for line in desc.splitlines() if len(line.strip()))
      desc = ''.join(filter(lambda x: x in string.printable, desc)) # remove non-printable characters
      csv_output.writerow([sha, vsdt, desc])


      This uses a multi-line expression that looks for blocks starting with Scanning. Where there are multiple lines, the lines are stripped and joined together using a |. Finally any non-printable characters are removed from the description.



      This would give you an output starting something like:



      SHA-1,VSDT,DESC
      004d44eeecae27314f8bd3825eb82d2f40182b51,WIN32 EXE 7-2,
      07eab9ea58d4669febf001d52c5182ecf579c407,WIN32 EXE 7-2,
      0d558bb5e0a5b544621af0ffde1940615ac39deb,WIN32 EXE 7-2,
      5172c70c1977bbddc2a163f6ede46595109c7835,WIN32 EXE 7-2,- $R0NsCpuCNMiner32.exe->Found Virus [WORM_CO.331300D2]|- $R0NsCpuCNMiner64.exe->Found Virus [WORM_CO.331300D2]|- $R0NsGpuCNMiner.exe->Found Virus [TROJ64_.743CC567]


      This assumes you are using Python 3.x






      share|improve this answer
























        0












        0








        0






        An alternative approach could be to just use a regular expression to extract whole blocks:



        import csv
        import re

        out_vsdt = "samples_vsdt.txt"
        out_sha1_vsdt = "sha1_vsdt.csv"

        with open(out_vsdt) as f_input:
        vscan32 = f_input.read()

        with open(out_sha1_vsdt, 'w', newline='') as f_output:
        csv_output = csv.writer(f_output)
        csv_output.writerow(['SHA-1', 'VSDT', 'DESC'])

        for sha, desc, vsdt in re.findall(r'Scanning.*?\([0-9a-f]+)(.*?)->((.*?))$', vscan32, re.S + re.M):
        desc = '|'.join(line.strip() for line in desc.splitlines() if len(line.strip()))
        desc = ''.join(filter(lambda x: x in string.printable, desc)) # remove non-printable characters
        csv_output.writerow([sha, vsdt, desc])


        This uses a multi-line expression that looks for blocks starting with Scanning. Where there are multiple lines, the lines are stripped and joined together using a |. Finally any non-printable characters are removed from the description.



        This would give you an output starting something like:



        SHA-1,VSDT,DESC
        004d44eeecae27314f8bd3825eb82d2f40182b51,WIN32 EXE 7-2,
        07eab9ea58d4669febf001d52c5182ecf579c407,WIN32 EXE 7-2,
        0d558bb5e0a5b544621af0ffde1940615ac39deb,WIN32 EXE 7-2,
        5172c70c1977bbddc2a163f6ede46595109c7835,WIN32 EXE 7-2,- $R0NsCpuCNMiner32.exe->Found Virus [WORM_CO.331300D2]|- $R0NsCpuCNMiner64.exe->Found Virus [WORM_CO.331300D2]|- $R0NsGpuCNMiner.exe->Found Virus [TROJ64_.743CC567]


        This assumes you are using Python 3.x






        share|improve this answer












        An alternative approach could be to just use a regular expression to extract whole blocks:



        import csv
        import re

        out_vsdt = "samples_vsdt.txt"
        out_sha1_vsdt = "sha1_vsdt.csv"

        with open(out_vsdt) as f_input:
        vscan32 = f_input.read()

        with open(out_sha1_vsdt, 'w', newline='') as f_output:
        csv_output = csv.writer(f_output)
        csv_output.writerow(['SHA-1', 'VSDT', 'DESC'])

        for sha, desc, vsdt in re.findall(r'Scanning.*?\([0-9a-f]+)(.*?)->((.*?))$', vscan32, re.S + re.M):
        desc = '|'.join(line.strip() for line in desc.splitlines() if len(line.strip()))
        desc = ''.join(filter(lambda x: x in string.printable, desc)) # remove non-printable characters
        csv_output.writerow([sha, vsdt, desc])


        This uses a multi-line expression that looks for blocks starting with Scanning. Where there are multiple lines, the lines are stripped and joined together using a |. Finally any non-printable characters are removed from the description.



        This would give you an output starting something like:



        SHA-1,VSDT,DESC
        004d44eeecae27314f8bd3825eb82d2f40182b51,WIN32 EXE 7-2,
        07eab9ea58d4669febf001d52c5182ecf579c407,WIN32 EXE 7-2,
        0d558bb5e0a5b544621af0ffde1940615ac39deb,WIN32 EXE 7-2,
        5172c70c1977bbddc2a163f6ede46595109c7835,WIN32 EXE 7-2,- $R0NsCpuCNMiner32.exe->Found Virus [WORM_CO.331300D2]|- $R0NsCpuCNMiner64.exe->Found Virus [WORM_CO.331300D2]|- $R0NsGpuCNMiner.exe->Found Virus [TROJ64_.743CC567]


        This assumes you are using Python 3.x







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 17:35









        Martin Evans

        27.3k132953




        27.3k132953






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388430%2fstopiteration-error-before-reading-the-text-file-using-next%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks