0

I want the input str to match with str in file that have fix row and then I will minus the score column of that row

1!! == i think this is for loop to find match str line by line from first to last

2!! == this is for when input str have matched it will minus score of matched row by 1.

CSV file:

enter image description here

article = pd.read_csv('Customer_List.txt', delimiter = ',',names = ['ID','NAME','LASTNAME','SCORE','TEL','PASS'])

y = len(article.ID)

line=article.readlines()

for x in range (0,y):  # 1!!

    if word in line :
       newarticle = int(article.SCORE[x]) - 1 #2!!       
       print(newarticle) 
    else: 
       x = x + 1

P.S. I have just study python for 5 days, please give me a suggestion.Thank you.

Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
beginner
  • 1
  • 2
  • Find it difficult to understand, what is your expected output? – Ankur Sinha Aug 03 '19 at 16:41
  • the step are 1. user input string 2. match input string to string in csv file (to find a row) 3.then delete score by 1 in score column of that row. this is output – beginner Aug 03 '19 at 16:45

1 Answers1

0

Since I see you using pandas, I will give a solution without any loops as it is much easier.

You have, for example:

df = pd.DataFrame()
df['ID'] = [216, 217]
df['NAME'] = ['Chatchai', 'Bigm']
df['LASTNAME'] = ['Karuna', 'Koratuboy']
df['SCORE'] = [25, 15]

You need to do:

lookfor = str(input("Enter the name: ")) 
df.loc[df.NAME == lookfor, 'SCORE']-= 1

What happens in the lines above is, you look for the name entered in the NAME column of your dataframe, and reduce the score by 1 if there is a match, which is what you want if I understand your question.

Example:

Now, let's say you are looking for a person called Alex with the name, since there is no such person, you must get the same dataframe back.

Enter the name: Alex
    ID      NAME   LASTNAME  SCORE
0  216  Chatchai     Karuna     25
1  217      Bigm  Koratuboy     15

Now, let's say you are looking for a person called Chatchai with the name, since there is a match and you want the score to be reduced, you will get: Enter the name: Chatchai

    ID      NAME   LASTNAME  SCORE
0  216  Chatchai     Karuna     24
1  217      Bigm  Koratuboy     15
Community
  • 1
  • 1
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
  • thank you so much. can i ask you a little bit more? I have to work with large of data file so i can't list all of the data in dataframe. What should i use ? – beginner Aug 03 '19 at 17:05
  • Sorry but I cannot understand your question. – Ankur Sinha Aug 03 '19 at 17:08
  • my csv file have a lot of lines , its very big file so i cant type all of the data in csv line. what should i do? – beginner Aug 03 '19 at 17:18
  • This link should help you :) https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe – Ankur Sinha Aug 03 '19 at 17:23