Python Forum
Unable to output the indices from an input file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to output the indices from an input file
#1
Hi everyone,

I'm trying to write a program to find all the indices of a value being searched from an input file. The program will accept the number to be searched for as 1st command line argument, and the name of the input file as the second argument. I'm having trouble to output the indices at which the value was found from the code below:

import sys

value = sys.argv[1]
file_name = sys.argv[2]
file = open(file_name, 'r')

print('The value of file \"{}\" to be searched:\n{}'.format(file_name, value))

for line in file.readlines():
curr_arr =
for i in line.split(','):
curr_arr +=[int(i)]

def find_values(curr_arr, val):
found_indexes =
sum = 0
for n in range(len(curr_arr)):
sum += curr_arr[n]
if curr_arr[n] == val:
found_indexes += [n]
return found_indexes

print 'The index(s) of the occurrence of {} in {}: {}'.format(value, curr_arr, find_values(curr_arr, value))


And this is what I get:

>>>a1.py 7 text.csv
>>>The value of file "text.csv" to be searched:
>>>7
The index(s) of the occurrence of 7 in [2, 3, 7, 9, 7, 3, 2]:

I supposed to get [2, 4] but it returns none. Could anyone help me with the code? Thank you
Reply
#2
Without a sample of "text.csv", we can not help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Output File ? Kessie1971 11 1,855 May-11-2023, 08:31 AM
Last Post: buran
  Need help implementing an input with file operations Gaijin 3 2,017 Jun-08-2022, 05:50 PM
Last Post: Gaijin
  how to add the user input from file into list wilson20 8 4,231 May-03-2020, 10:52 PM
Last Post: Larz60+
  reading text file and writing to an output file precedded by line numbers kannan 7 10,248 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Code to use output from arduino as input (help) Updownrightleft 0 2,205 Nov-03-2018, 11:04 PM
Last Post: Updownrightleft
  Multiple XML file covert to CSV output file krish143 1 3,310 Jul-27-2018, 06:55 PM
Last Post: ichabod801
  How to get output in a file? BananaRekt 3 3,070 May-06-2018, 06:04 PM
Last Post: buran
  Unable to create csv file Sumaira 2 3,209 Feb-06-2018, 03:43 AM
Last Post: Sumaira
  Saving the results from an input in a txt. file pythonenthusiast 2 2,646 Nov-28-2017, 03:52 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020