0

I'm having some difficulties with my code - wondering if anyone could help me as to where I'm going wrong.

The general syntax of the goal I'm trying to achieve is:

  1. Get user input
  2. Split input into individual variables
  3. Write variables (amend) to 'data.csv'
  4. Read variables from newly amended 'data.csv'
  5. Add variables to list
  6. If variable 1 <= length of list, #run some code
  7. If variable 2 <= length of list, #run some code

Here is my python code:

from selenium import webdriver
import time
import csv

x = raw_input("Enter numbers separated by a space")
integers = [[int(i)] for i in x.split()]

with open("data.csv", "a") as f:
   writer = csv.writer(f)
   writer.writerows(integers)

with open('data.csv', 'r') as f:
   file_contents = f.read()
   previous_FONs = file_contents.split(' ')

if list.count(integers[i]) == 1:
   #run some code

elif list.count(integers[i]) == 2:
   #run some code

The error message I'm receiving is TypeError: count() takes exactly one argument (0 given)

booooooky
  • 31
  • 6
  • print out integers[i] and see what you get. – marxmacher Jan 09 '20 at 10:54
  • @marxmacher when I print this I get just the first variable on the data.csv file [111] – booooooky Jan 09 '20 at 10:57
  • why are you doing list.count(blah) ? what is this list? – marxmacher Jan 09 '20 at 11:01
  • where is the i even coming from for the list.count(integers[i]) if it try to run the code ommiting the append and read of csv just take input make a list of list (like you are doing) and then try calling list.count(integers[i] i get a bunch of problems – marxmacher Jan 09 '20 at 11:02
  • @marxmacher I'm trying to do a list count to check how many times the user input appears in the csv file – booooooky Jan 09 '20 at 11:04
  • To verify length just use `len(integers[i]) == 1` – KunduK Jan 09 '20 at 11:04
  • 1
    did you mean to write integers.count(some_item) == 1 ? – marxmacher Jan 09 '20 at 11:06
  • also the var i only exists here : integers = [[int(i)] for i in x.split()] how can you use it outside it in the later list.count? – marxmacher Jan 09 '20 at 11:07
  • @marxmacher should that list count be on the `previous_FONs` rather than `integers[i]` – booooooky Jan 09 '20 at 11:51
  • i did not understand your question. the questions point 1-7 are also a bit confusing. DO you want to have a user input a list of variable over which you will iterate in a for loop to check how many of what integare are there and it the count of any of the integares is either 1 or 2 then execute code? – marxmacher Jan 09 '20 at 12:07
  • @marxmacher apologies. What I want is to have a user input some integers (for example '111 222 333'), and have the code amend this to the 'data.csv' file. The code will then for loop through each input integer and count how many times it occurs in the csv file (so in this example the first integer checked will be '111') - if it appears once, run this code, if it appears twice run that code etc. It will then run cycle to the second user input (in this example '222') and do the same - if it appears in the csv file once, run this code, it it appears twice, run that code. Does this make sense? – booooooky Jan 09 '20 at 12:18
  • maybe then this here will be helpful? https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item – marxmacher Jan 09 '20 at 12:41

1 Answers1

0

Because of the following line

integers = [[int(i)] for i in x.split()]

you're creating a list of lists. Therefore you're passing lists to the count method. Try this one:

integers = [int(i) for i in x.split()]


Edit: Based on your explanation what you want to achieve, this code should do it:
import csv

x = raw_input('Enter numbers separated by a space: ')
new_FONs = [[int(i)] for i in x.split()]

with open('data.csv', 'a', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(new_FONs)

with open('data.csv', 'r') as f:
    all_FONs_str = [line.split() for line in f]
    all_FONs = [[int(FON[0])] for FON in all_FONs_str]

# For each of the user-input numbers
for FON in new_FONs:

    # Count the occurance of this number in the CSV file
    FON_count = all_FONs.count(FON)

    if FON_count == 1:
        print(f'{FON[0]} occurs once')
        # do stuff

    elif FON_count == 2:
        print(f'{FON[0]} occurs twice')
        # do stuff

I have changed the name of the list read from CSV to all_FONs just to remind that this contains the old entries and also the new once (as we wrote them into the file before reading).

In addition you need to convert the entries as when reading from CSV you get strings not integers, what would make the comparison difficult. Maybe the whole conversion to int is not necessary, just work on strings. But that depends on what you need.


Edit2: Sorry forgot to change from input to raw_input for Python 2.7 :)
themaksmw
  • 25
  • 6
  • just tried this but now I'm getting `Error: sequence expected` on line `writer.writerows(integers)` – booooooky Jan 09 '20 at 11:36
  • Ok so this depends on integers being a list of lists. How about you change `integers[i]` in the last lines to `integers[i][0]` in order to access the first element of each list for count? – themaksmw Jan 09 '20 at 12:43
  • @booooooky How do you want the new user inputs to be added to the CSV? Each number a new line, or all in one line, or sth else? – themaksmw Jan 09 '20 at 13:05
  • each number a new line if possible – booooooky Jan 09 '20 at 13:06
  • I just tried your code (massive thankyou btw!) but am receiving the following error: Traceback (most recent call last): File "", line 3, in File "", line 1 111 222 333 ^ SyntaxError: invalid syntax – booooooky Jan 09 '20 at 13:38
  • @booooooky did you try to use a new CSV file? – themaksmw Jan 09 '20 at 13:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205677/discussion-between-themaksmw-and-booooooky). – themaksmw Jan 09 '20 at 13:51