Trouble reading a file in python












-2















I'm trying to read a file but for some reason it is not working and I have some trouble to diagnose the problem as I'm not used to python.



I use the following code to read the file



with open(self.lineEdit_2.text(), 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
idList =
idExist = false
for row in reader:
print(row[1])
for i in len(idList):
if row[1] == idList[i]:
idExist = true
break
else:
idExist = false
if not idExist:
idList.append(row[1])


The file is called can_data.txt and self.lineEdit_2.text() return the destination to that file. The file has the following structure:



1542208616,0x7DF,8,01 03 55 55 55 55 55 55 
1542208616,0x7E9,8,02 43 00 00 00 00 00 00
1542208616,0x7E8,8,02 43 00 00 00 00 00 00
1542208616,0x7DF,8,01 07 55 55 55 55 55 55
1542208616,0x7E9,8,02 47 00 00 00 00 00 00
1542208616,0x7E8,8,02 47 00 00 00 00 00 00


The purpose is to list the different ids that is inside the file in column 1.



While trying to debug it exit when it reach for row in reader:










share|improve this question

























  • What exactly is your intention with this code?

    – Helena Martins
    Nov 20 '18 at 11:13











  • I think the last two lines should be indented to be inside the main for loop.

    – myrmica
    Nov 20 '18 at 11:16











  • How do you know it is not working? (Do not reply to this comment; it is a suggestion to edit your post.)

    – usr2564301
    Nov 20 '18 at 11:23











  • The inner for loop can be avoided by using a containment test (e.g. if row[1] in idList). Also, True and False must be capitalized in Python.

    – myrmica
    Nov 20 '18 at 11:29
















-2















I'm trying to read a file but for some reason it is not working and I have some trouble to diagnose the problem as I'm not used to python.



I use the following code to read the file



with open(self.lineEdit_2.text(), 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
idList =
idExist = false
for row in reader:
print(row[1])
for i in len(idList):
if row[1] == idList[i]:
idExist = true
break
else:
idExist = false
if not idExist:
idList.append(row[1])


The file is called can_data.txt and self.lineEdit_2.text() return the destination to that file. The file has the following structure:



1542208616,0x7DF,8,01 03 55 55 55 55 55 55 
1542208616,0x7E9,8,02 43 00 00 00 00 00 00
1542208616,0x7E8,8,02 43 00 00 00 00 00 00
1542208616,0x7DF,8,01 07 55 55 55 55 55 55
1542208616,0x7E9,8,02 47 00 00 00 00 00 00
1542208616,0x7E8,8,02 47 00 00 00 00 00 00


The purpose is to list the different ids that is inside the file in column 1.



While trying to debug it exit when it reach for row in reader:










share|improve this question

























  • What exactly is your intention with this code?

    – Helena Martins
    Nov 20 '18 at 11:13











  • I think the last two lines should be indented to be inside the main for loop.

    – myrmica
    Nov 20 '18 at 11:16











  • How do you know it is not working? (Do not reply to this comment; it is a suggestion to edit your post.)

    – usr2564301
    Nov 20 '18 at 11:23











  • The inner for loop can be avoided by using a containment test (e.g. if row[1] in idList). Also, True and False must be capitalized in Python.

    – myrmica
    Nov 20 '18 at 11:29














-2












-2








-2








I'm trying to read a file but for some reason it is not working and I have some trouble to diagnose the problem as I'm not used to python.



I use the following code to read the file



with open(self.lineEdit_2.text(), 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
idList =
idExist = false
for row in reader:
print(row[1])
for i in len(idList):
if row[1] == idList[i]:
idExist = true
break
else:
idExist = false
if not idExist:
idList.append(row[1])


The file is called can_data.txt and self.lineEdit_2.text() return the destination to that file. The file has the following structure:



1542208616,0x7DF,8,01 03 55 55 55 55 55 55 
1542208616,0x7E9,8,02 43 00 00 00 00 00 00
1542208616,0x7E8,8,02 43 00 00 00 00 00 00
1542208616,0x7DF,8,01 07 55 55 55 55 55 55
1542208616,0x7E9,8,02 47 00 00 00 00 00 00
1542208616,0x7E8,8,02 47 00 00 00 00 00 00


The purpose is to list the different ids that is inside the file in column 1.



While trying to debug it exit when it reach for row in reader:










share|improve this question
















I'm trying to read a file but for some reason it is not working and I have some trouble to diagnose the problem as I'm not used to python.



I use the following code to read the file



with open(self.lineEdit_2.text(), 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
idList =
idExist = false
for row in reader:
print(row[1])
for i in len(idList):
if row[1] == idList[i]:
idExist = true
break
else:
idExist = false
if not idExist:
idList.append(row[1])


The file is called can_data.txt and self.lineEdit_2.text() return the destination to that file. The file has the following structure:



1542208616,0x7DF,8,01 03 55 55 55 55 55 55 
1542208616,0x7E9,8,02 43 00 00 00 00 00 00
1542208616,0x7E8,8,02 43 00 00 00 00 00 00
1542208616,0x7DF,8,01 07 55 55 55 55 55 55
1542208616,0x7E9,8,02 47 00 00 00 00 00 00
1542208616,0x7E8,8,02 47 00 00 00 00 00 00


The purpose is to list the different ids that is inside the file in column 1.



While trying to debug it exit when it reach for row in reader:







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 21:21









marc_s

577k12911141259




577k12911141259










asked Nov 20 '18 at 11:10









Jhonny MattheuJhonny Mattheu

32




32













  • What exactly is your intention with this code?

    – Helena Martins
    Nov 20 '18 at 11:13











  • I think the last two lines should be indented to be inside the main for loop.

    – myrmica
    Nov 20 '18 at 11:16











  • How do you know it is not working? (Do not reply to this comment; it is a suggestion to edit your post.)

    – usr2564301
    Nov 20 '18 at 11:23











  • The inner for loop can be avoided by using a containment test (e.g. if row[1] in idList). Also, True and False must be capitalized in Python.

    – myrmica
    Nov 20 '18 at 11:29



















  • What exactly is your intention with this code?

    – Helena Martins
    Nov 20 '18 at 11:13











  • I think the last two lines should be indented to be inside the main for loop.

    – myrmica
    Nov 20 '18 at 11:16











  • How do you know it is not working? (Do not reply to this comment; it is a suggestion to edit your post.)

    – usr2564301
    Nov 20 '18 at 11:23











  • The inner for loop can be avoided by using a containment test (e.g. if row[1] in idList). Also, True and False must be capitalized in Python.

    – myrmica
    Nov 20 '18 at 11:29

















What exactly is your intention with this code?

– Helena Martins
Nov 20 '18 at 11:13





What exactly is your intention with this code?

– Helena Martins
Nov 20 '18 at 11:13













I think the last two lines should be indented to be inside the main for loop.

– myrmica
Nov 20 '18 at 11:16





I think the last two lines should be indented to be inside the main for loop.

– myrmica
Nov 20 '18 at 11:16













How do you know it is not working? (Do not reply to this comment; it is a suggestion to edit your post.)

– usr2564301
Nov 20 '18 at 11:23





How do you know it is not working? (Do not reply to this comment; it is a suggestion to edit your post.)

– usr2564301
Nov 20 '18 at 11:23













The inner for loop can be avoided by using a containment test (e.g. if row[1] in idList). Also, True and False must be capitalized in Python.

– myrmica
Nov 20 '18 at 11:29





The inner for loop can be avoided by using a containment test (e.g. if row[1] in idList). Also, True and False must be capitalized in Python.

– myrmica
Nov 20 '18 at 11:29












2 Answers
2






active

oldest

votes


















0














Do you have a constraint on which package you use to read your files?
If not, why not try pandas?



From what I understand from your script, the second column contains ids, and you what a list of all different ids that appear in your data.



You can therefore try this (my guess is that it will be faaar more efficient):



import pandas as pd
df = pd.read_table(self.lineEdit_2.text() , sep=',', names=['c0', 'id', 'c2', 'c3', 'c4', 'c5', 'c6'])
unique_ids = df['id'].unique()





share|improve this answer
























  • This one is really awesome and easy to use! Thanks for a nice suggestion.

    – Jhonny Mattheu
    Nov 21 '18 at 13:26



















0














Ok, first of all: fix your indentation! After with ...: there should always be an indented block.



Secondly what does self.lineEdit_2.text() return? The open() method expects the path of the file as it's first argument, not the text you're trying to read... (https://docs.python.org/3/library/functions.html#open)



My guess is that you don't need the first line at all but something like this might work:



reader = csv.reader(self.lineEdit_2.text().split('n'), delimiter=',')
for row in reader:
...





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%2f53391709%2ftrouble-reading-a-file-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Do you have a constraint on which package you use to read your files?
    If not, why not try pandas?



    From what I understand from your script, the second column contains ids, and you what a list of all different ids that appear in your data.



    You can therefore try this (my guess is that it will be faaar more efficient):



    import pandas as pd
    df = pd.read_table(self.lineEdit_2.text() , sep=',', names=['c0', 'id', 'c2', 'c3', 'c4', 'c5', 'c6'])
    unique_ids = df['id'].unique()





    share|improve this answer
























    • This one is really awesome and easy to use! Thanks for a nice suggestion.

      – Jhonny Mattheu
      Nov 21 '18 at 13:26
















    0














    Do you have a constraint on which package you use to read your files?
    If not, why not try pandas?



    From what I understand from your script, the second column contains ids, and you what a list of all different ids that appear in your data.



    You can therefore try this (my guess is that it will be faaar more efficient):



    import pandas as pd
    df = pd.read_table(self.lineEdit_2.text() , sep=',', names=['c0', 'id', 'c2', 'c3', 'c4', 'c5', 'c6'])
    unique_ids = df['id'].unique()





    share|improve this answer
























    • This one is really awesome and easy to use! Thanks for a nice suggestion.

      – Jhonny Mattheu
      Nov 21 '18 at 13:26














    0












    0








    0







    Do you have a constraint on which package you use to read your files?
    If not, why not try pandas?



    From what I understand from your script, the second column contains ids, and you what a list of all different ids that appear in your data.



    You can therefore try this (my guess is that it will be faaar more efficient):



    import pandas as pd
    df = pd.read_table(self.lineEdit_2.text() , sep=',', names=['c0', 'id', 'c2', 'c3', 'c4', 'c5', 'c6'])
    unique_ids = df['id'].unique()





    share|improve this answer













    Do you have a constraint on which package you use to read your files?
    If not, why not try pandas?



    From what I understand from your script, the second column contains ids, and you what a list of all different ids that appear in your data.



    You can therefore try this (my guess is that it will be faaar more efficient):



    import pandas as pd
    df = pd.read_table(self.lineEdit_2.text() , sep=',', names=['c0', 'id', 'c2', 'c3', 'c4', 'c5', 'c6'])
    unique_ids = df['id'].unique()






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 11:22









    Matina GMatina G

    577211




    577211













    • This one is really awesome and easy to use! Thanks for a nice suggestion.

      – Jhonny Mattheu
      Nov 21 '18 at 13:26



















    • This one is really awesome and easy to use! Thanks for a nice suggestion.

      – Jhonny Mattheu
      Nov 21 '18 at 13:26

















    This one is really awesome and easy to use! Thanks for a nice suggestion.

    – Jhonny Mattheu
    Nov 21 '18 at 13:26





    This one is really awesome and easy to use! Thanks for a nice suggestion.

    – Jhonny Mattheu
    Nov 21 '18 at 13:26













    0














    Ok, first of all: fix your indentation! After with ...: there should always be an indented block.



    Secondly what does self.lineEdit_2.text() return? The open() method expects the path of the file as it's first argument, not the text you're trying to read... (https://docs.python.org/3/library/functions.html#open)



    My guess is that you don't need the first line at all but something like this might work:



    reader = csv.reader(self.lineEdit_2.text().split('n'), delimiter=',')
    for row in reader:
    ...





    share|improve this answer




























      0














      Ok, first of all: fix your indentation! After with ...: there should always be an indented block.



      Secondly what does self.lineEdit_2.text() return? The open() method expects the path of the file as it's first argument, not the text you're trying to read... (https://docs.python.org/3/library/functions.html#open)



      My guess is that you don't need the first line at all but something like this might work:



      reader = csv.reader(self.lineEdit_2.text().split('n'), delimiter=',')
      for row in reader:
      ...





      share|improve this answer


























        0












        0








        0







        Ok, first of all: fix your indentation! After with ...: there should always be an indented block.



        Secondly what does self.lineEdit_2.text() return? The open() method expects the path of the file as it's first argument, not the text you're trying to read... (https://docs.python.org/3/library/functions.html#open)



        My guess is that you don't need the first line at all but something like this might work:



        reader = csv.reader(self.lineEdit_2.text().split('n'), delimiter=',')
        for row in reader:
        ...





        share|improve this answer













        Ok, first of all: fix your indentation! After with ...: there should always be an indented block.



        Secondly what does self.lineEdit_2.text() return? The open() method expects the path of the file as it's first argument, not the text you're trying to read... (https://docs.python.org/3/library/functions.html#open)



        My guess is that you don't need the first line at all but something like this might work:



        reader = csv.reader(self.lineEdit_2.text().split('n'), delimiter=',')
        for row in reader:
        ...






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 11:18









        Gijs WobbenGijs Wobben

        515




        515






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391709%2ftrouble-reading-a-file-in-python%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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?