Question

I would like a python code for this problem please thank you! We have a CSV...

I would like a python code for this problem please thank you!
We have a CSV format file containing a table of a list of grocery stores such as ABC, YUH, NJDNHD, ZRTHGI etc. We want to split the file into 2 files with 1 file containing only 1 grocery store( for example ADHKO) and the other file containing all other grocery stores. You cannot use the first character or length method as 2 grocery stores can start with the same character and have the same number of characters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import csv #for writing csv file

#file that holds the grocery store name
fileName="groceryStore.csv"
#array for storing the grocery store name
storeName=[]
#open the file in read mode as variable file
with open(fileName,"r") as file:
#read file, split it and store the splitted grocercy store name in list storeName
storeName=file.read().split(',')
#take the users choice store name
choiceStore=input("Enter the store of your choice: ")
check=0
#for loop to find whether uers choice exist in the file
for i in storeName:
#if users choice is equal to the array element
if(i==choiceStore):
#set check to 1
check=1
break

#if check is equal to zero meaning user choice not found
if(check==0):
print("The store name is not presnet in file")
else:#user choice found
#remove the user choosed store from storeName list
storeName.remove(choiceStore)
#file1.csv will store the user choice store name
#open file1.csv in write mode
with open('file1.csv','w') as csvfile:
#set csv writer o file1
writer=csv.writer(csvfile)
#witer users choosen store
writer.writerow([choiceStore])
#file2.csv will store the remaining store name
#open file2 in write mode
with open("file2.csv","w") as csvfile:
#set csv writer on file2
writer=csv.writer(csvfile)
#write list on file2
writer.writerow(storeName)
print("File splitted")

OUTPUT:

Please thumbs-up / vote up this answer if it was helpful. In case of any problem, please comment below. I will surely help. Down-votes are permanent and not notified to us, so we can't help in that case.

Add a comment
Know the answer?
Add Answer to:
I would like a python code for this problem please thank you! We have a CSV...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT