Related
I'm trying to end the code, but I have problem how to slice data frame based on user's input. Is there any option to do this without pandas?
def dataSet_read():
enter = input('Enter file path:')
csvreader = csv.reader(open(enter))
head_inp = input('Has the file headers? Select Y or N:\n').upper()
header = []
if head_inp == 'Y':
header = next(csvreader)
print('\nFile headers:\n\n', header)
elif head_inp == 'N':
print("'\nFile doesn't have headers")
else:
print('Incorrect selection!!!')
sys.exit()
with open(str(enter), "r") as csvfile:
reader_variable = csv.reader(csvfile, delimiter = ",")
rows_inp = input("\nPlease provide range which you'd like to see using ',', otherwise all dataframe will open all dataset.\n")
if rows_inp == '':
for row in reader_variable:
print(row)
else:
print("????")
cast it to list then you can slice like what it is in normal list structure.
enter = input('Enter file path:')
rows_inp = input("slice")
with open(enter , 'r') as f:
reader_variable = csv.reader(f)
reader_list= list(reader_variable)
for row in reader_list[:rows_inp]:#if you want slice the whole data
current_date = row[:rows_inp] #if you want slice per row
print(current_date)
I found the way to get what I need, maybe it's not the best approach but works :)
with open(str(enter), "r") as csvfile:
reader_variable = csv.reader(csvfile, delimiter = ",")
rows_inp = input("\nPlease provide range which you'd like to see using ',', otherwise all dataframe will open all dataset.\n")
if rows_inp == '':
for row in reader_variable:
print(row)
else:
i, j = rows_inp.split(',')
reader_list = list(reader_variable)
print(reader_list[int(i):int(j)+1])
I have three CSV files:
doctors.csv
1,John,Smith,Internal Med
2,Jone,Smith,Pediatrics
3,Jone,Carlos,Cardiology
patients.csv
1,Sara,Smith,20,07012345678,B1234
2,Mike,Jones,37,07555551234,L22AB
3,Daivd,Smith,15,07123456789,C1ABC
... and linked.csv, which I need to populate based on doctors.csv and patients.csv.
I'm taking two inputs from the user that correspond with the doctor ID and patient ID, and checking if they are present, and then writing them to the linked.csv file.
I'd like the linked.csv file to contain for each column:
[patientID,patientfirstname,patientsurname,doctorID,doctorfirstname,doctorlastname]
Unfortunately, I can't figure out how to read a specific row using the csv module and then extract the specific data I need from both files.
Here is the code I have so far:
#asking for input
print('Please select both a Patient ID and Doctor ID to link together')
patient_index = input('Please enter the patient ID: ')
doctorlink = input('Please select a doctor ID: ')
doctorpresent = False
patientpresent = False
# precence check for both values
with open('patiens.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
if patient_index == row[0]:
print('Patient is present')
patientpresent = True
with open('doctors.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
if patient_index == row[0]:
print('Doctor is present')
doctorpresent = True
if patientpresent == True and doctorpresent == True:
Here, I need to add the code necessary for extracting the rows.
I have used CSV library to read files from both files and combine them into a list.
import csv
# asking for input
print('Please select both a Patient ID and Doctor ID to link together')
patient_index = input('Please enter the patient ID: ')
doctor_index = input('Please select a doctor ID: ')
output = []
# opening the CSV file
with open('patient.csv', 'r') as f:
# reading the CSV file
reader = csv.reader(f, delimiter=',')
# iterating the rows
for row in reader:
if patient_index == row[0]:
output.append(row[0]) # appending patientID
output.append(row[1]) # appending patientfirstname
output.append(row[2]) # appending patientlastname
# opening the CSV file
with open('doctor.csv', 'r') as f:
# reading the CSV file
reader = csv.reader(f, delimiter=',')
# iterating the rows
for row in reader:
if doctor_index == row[0]:
output.append(row[0]) # appending doctorID
output.append(row[1]) # appending doctorfirstname
output.append(row[2]) # appending doctorlastname
print(output)
Output
['1', 'Sara', 'Smith', '1', 'John', 'Smith']
[patientID,patientfirstname,patientsurname,doctorID,doctorfirstname,doctorlastname]
Hope this helps. Happy Coding :)
This looks like a task better suited for a database, but here is a possible solution just using the csv module (pathlib here is just my preferred way to handle files):
import csv
from pathlib import Path
# Files
patients_csv = Path('patients.csv')
doctors_csv = Path('doctors.csv')
linked_csv = Path('linked.csv')
# Create in memory lists for patients and doctors
with patients_csv.open() as f:
reader = csv.reader(f)
patients = [_ for _ in reader]
with doctors_csv.open() as f:
reader = csv.reader(f)
doctors = [_ for _ in reader]
print('Please select both a Patient ID and Doctor ID to link together')
patient_id = input('Please enter the patient ID: ')
doctor_id = input('Please select a doctor ID: ')
# Is there a doctor that matches doctor_id?
doctor = [_ for _ in doctors if _[0] == doctor_id]
# Is there a patient that matches patient_id?
patient = [_ for _ in patients if _[0] == patient_id]
# Do we have a patient and a doctor match?
if patientt and doctor:
patient = patient[0]
doctor = doctor[0]
with linked_csv.open('a') as f:
print(*patient, *doctor, file=f, sep=',')
And here is the result of a test run of the code above, which is in app.py here:
$ ls
app.py doctors.csv patients.csv
$ cat doctors.csv
1,John,Smith,Internal Med
2,Jone,Smith,Pediatrics
3,Jone,Carlos,Cardiology
$ cat patients.csv
1,Sara,Smith,20,07012345678,B1234
2,Mike,Jones,37,07555551234,L22AB
3,Daivd,Smith,15,07123456789,C1ABC
$ python app.py
Please select both a Patient ID and Doctor ID to link together
Please enter the patient ID: 1
Please select a doctor ID: 1
$ cat linked.csv
1,Sara,Smith,20,07012345678,B1234,1,John,Smith,Internal Med
It can also be implemented independent of the csv module:
from pathlib import Path
# Files
patients_csv = Path('patients.csv')
doctors_csv = Path('doctors.csv')
linked_csv = Path('linked.csv')
# Create in memory lists for patients and doctors
with patients_csv.open() as f:
patients = [_.strip().split(',') for _ in f]
with doctors_csv.open() as f:
doctors = [_.strip().split(',') for _ in f]
print('Please select both a Patient ID and Doctor ID to link together')
patient_id = input('Please enter the patient ID: ')
doctor_id = input('Please select a doctor ID: ')
# Is there a doctor that matches doctor_id?
doctor = [_ for _ in doctors if _[0] == doctor_id]
# Is there a patient that matches patient_id?
patient = [_ for _ in patients if _[0] == patient_id]
# Do we have a patient and a doctor match?
if patientt and doctor:
patient = patient[0]
doctor = doctor[0]
with linked_csv.open('a') as f:
print(*patient, *doctor, file=f, sep=',')
Both examples are just starting points, which you can construct and improve above them.
I am beginning to learn python and I have this project where I have a menu that either adds, lists, or updates values for books in a csv. The headers are "BookName", "AuthorName", "SharedWith", "IsRead" and I am trying to change the "IsRead" row for a given book added by the user.
My problem is that whenever I am trying to edit the row that contains that book, I end up deleting all other rows instead. All I want to do is to update a value for a certain row in a csv. Here's the function that I wrote.
def updateBook():
book_name = input("Enter book name: ")
import csv
rows_list = []
with open('booksDB.csv', mode='r') as file:
rows = list(csv.DictReader(file, fieldnames=("BookName", "AuthorName", "SharedWith", "IsRead")))
for row in rows:
rows_list.append(row["BookName"]) # we store every book name in a list
if book_name not in rows_list: # we search the book the user typed in our list
add_new_book = input(f' The {book_name} book does not exits. Would you like to add it? (Y/N)? ')
if add_new_book.upper() == "N":
return
else:
addBook()
return
else:
book_read = input("Is the book read? (Y/N)? ")
if book_read.upper() == 'Y':
book_read = True
else:
book_read = False
rows = []
with open('booksDB.csv', mode='r') as file:
rows = list(csv.DictReader(file, fieldnames=("BookName", "AuthorName", "SharedWith", "IsRead")))
for row in rows:
if row["BookName"] == book_name:
row["IsRead"] = book_read
break
with open('booksDB.csv',mode='r+') as file: # WIP, here I can't make the csv keep all books in the list when editing a certain one
csv_writer = csv.DictWriter(file, fieldnames=[
"BookName", "AuthorName", "SharedWith", "IsRead"
])
if row["IsRead"] == book_read:
csv_writer.writerow({"BookName": row.get("BookName"),
"AuthorName": row.get("AuthorName"),
"SharedWith": row.get("SharedWith"),
"IsRead": book_read})
print("Book was updated successfully")
The First Thing You Should Open Your CSV File In a 'w' Mode Not 'r'
After I Looked at your Code You Didn't Respect The Indentation For your Function Try To Fix it First
Did You Try To Give Input First
roww = input("Enter The Name Of The Row Here : ")
and After use it :
for row in rows:
if row["BookName"] == book_name:
row[rf"{roww}"] = book_read
break
Try This it Might Help
Thank you guys for your support. I made a small tweak and managed to get the code working 100% with the following lines:
def updateBook():
book_name = input("Enter book name: ")
import csv
rows = []
rows_list = []
with open('booksDB.csv', mode='r') as file:
rows = list(csv.DictReader(file, fieldnames=("BookName", "AuthorName", "SharedWith", "IsRead")))
for row in rows:
rows_list.append(row["BookName"]) # we store every book name in a list
if book_name not in rows_list: # we search the book the user typed in our list
add_new_book = input(f' The {book_name} book does not exits. Would you like to add it? (Y/N)? ')
if add_new_book.upper() == "N":
return
else:
addBook()
return
else:
book_read = input("Is the book read? (Y/N)? ")
if book_read.upper() == 'Y':
book_read = True
else:
book_read = False
rows = []
with open('booksDB.csv', mode='r') as file:
rows = list(csv.DictReader(file, fieldnames=("BookName", "AuthorName", "SharedWith", "IsRead")))
for row in rows:
if row["BookName"] == book_name:
row["IsRead"] = book_read
break
with open('booksDB.csv',mode='w') as file:
csv_writer = csv.DictWriter(file, fieldnames=["BookName", "AuthorName", "SharedWith", "IsRead"])
csv_writer.writerows(rows)
print("Book was updated successfully!")
I have a problem , i wanna to search a data with python from csv file
my code like this
#search process area
area_proses = []
sg1 = []
sg2 = []
sg3 = []
avg = []
#input number you want to search
number = raw_input('Masukan id Spesific Goal\n')
#read csv, and split on "," the line
csv_file = csv.reader(open('C:/xampp_2/htdocs/SkripsiV2/fuzzy/download.csv', "rb"), delimiter=",")
#loop through csv list
for row in csv_file:
area_proses.append(row[1])
sg1.append(row[2])
sg2.append(row[3])
sg3.append(row[4])
avg.append(row[5])
#if current rows 1nd value is equal to input, print that row
if number == row[0]:
#masukan data
print(area_proses,sg1,sg2,sg3,avg)
my problem is when i search with id 11 the output is like this:
(['area_proses', 'Service Delivery'], ['sg1', '3.71'], ['sg2', '3.48'], ['sg3',
'3.30'], ['avg', '3.50'])
but when i search id 12 the output is like :
(['area_proses', 'Service Delivery', 'Incident Resolution and Prevention'], ['sg
1', '3.71', '3.83'], ['sg2', '3.48', '3.65'], ['sg3', '3.30', '3.70'], ['avg', '
3.50', '3.73'])
How i can solved this problem?
Download.csv
"id","area_proses","sg1","sg2","sg3","avg","fuzzy",
"11","Service Delivery","3.71","3.48","3.30","3.50","0.00000000000",
"12","Incident Resolution and Prevention","3.83","3.65","3.70","3.73","0.00000000000",
"13","Service System Development","3.93","3.29","3.26","3.49","0.00000000000",
"14","Service System Transition","3.00","3.43","0.00","3.22","0.00000000000",
"15","Strategic Service Management","3.48","3.86","0.00","3.67","0.00000000000",
"16","Configuration Management","3.14","3.57","0.00","3.36","0.00000000000",
"17","Measurement and Analysis","2.93","3.18","0.00","3.06","0.00000000000",
Try using the pandas library. Install it, then do:
import pandas as pd
df = pd.read_csv('csv_file.csv')
df[df['id'] == number]
Just change 'rb' to 'r'
fopn = open(file_loc, "r")
csv_file = csv.reader(fopn)
for row in csv_file:
if number == row[0]:
print(row)
I'm writing a python script that works with two csv files. Lets call them csv1.csv (original file to read) and csv2.csv (exact copy of csv1). The goal is to find the row and column in the csv file that corresponds to the the modified user-defined input.
csv format:(continues for about 2-3 thousand lines)
record LNLIM, ID_CO,OD_DV,ID_LN, ST_LN, ZST_LN, ID_LNLIM,LIMIT1_LNLIM, LIMIT2_LNLIM, LIMIT3_LNLIM
LNLIM, 'FPL', 'SOUT', '137TH_LEVEE_B', 'B', '137TH_AV', 'LEVEE', 'A', 1000, 1100, 1200
LNLIM, 'FPL', 'SOUT', '137TH_DAVIS_B', 'A', '137TH_AV', 'NEWTON', 'A', 1000, 1100, 1200
...
Let's say that the user is looking for 137TH_AV and NEWTON. I want to be able to go row by row and compare the two columns/row indices ST_LN and ZST_LN. If both columns match what the user inputted then I want to capture which row in the csv file that happened on, and use that information to edit the remaining columns LIMIT1_LNLIM LIMIT2_LNLIM LIMIT3_LNLIM on that row with new analog values.
I want to get the 3 new values provided by the user and edit a specific row, and a specific row element. Once I've found the place to replace the number values I want to overwrite csv2.csv with this edit.
Determining where the line segment is located in the array
import sys
import csv
import os
import shutil
LineSectionNames = []
ScadaNames = []
with open('Vulcan_Imp_Summary.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
LineSectionName = row[1]
ScadaName = row[29]
LineSectionNames.append(LineSectionName)
ScadaNames.append(ScadaName)
#Reformatting arrays for accurate references
LineSectionNames = [character.replace('\xa0', ' ') for character in LineSectionNames]
LineSectionNames = [character.replace('?', '-') for character in LineSectionNames]
ScadaNames = [character.replace('\xa0', ' ') for character in ScadaNames]
#Setting Line Section name as key and Scada name as value
ScadaDict = {}
for i in range(len(LineSectionNames)):
ScadaDict[LineSectionNames[i]] = ScadaNames[i]
#Prompt user for grammatical name of Line Section
print ('Enter the Line Section Name: (Example = Goulds-Princeton) \n')
user_input = input()
#Reference user input to dictionary value to convert input into SCADA format
def reformat():
print ('Searching for Line Section...' + user_input)
if user_input in ScadaDict:
value = ScadaDict[user_input]
print ('\n\t Match!\n')
else:
print ('The Line Section name you have entered was incorrect. Try again. \n Example = Goulds-Princeton')
reformat()
# Copying the exported file from Genesys
path = 'I://PSCO//DBGROUP//PatrickL//'
shutil.copyfile(path + 'lnlim_import.csv', path + 'lnlim_import_c.csv')
#Using the SCADA format to search through csv file
print ('Searching csv file for...' + user_input)
# Reading the copied file
record_lnlims = []
id_cos = []
id_dvs = []
id_lines = []
id_lns = []
st_lns = []
zst_lns = []
id_lnlims = []
limit1_lnlims = []
limit2_lnlims = []
limit3_lnlims = []
with open('lnlim_import_c.csv', 'r') as copy:
reader = csv.reader(copy)
for row in reader:
record_lnlim = row[0]
id_co = row[1]
id_dv = row[2]
id_line = row[3]
id_ln = row[4]
st_ln = row[5]
zst_ln = row[6]
id_lnlim = row[7]
limit1_lnlim = row[8]
limit2_lnlim = row[9]
limit3_lnlim = row[10]
record_lnlims.append(record_lnlim)
id_cos.append(id_co)
id_dvs.append(id_dv)
id_lines.append(id_line)
id_lns.append(id_ln)
st_lns.append(st_ln)
zst_lns.append(zst_ln)
id_lnlims.append(id_lnlim)
limit1_lnlims.append(limit1_lnlim)
limit2_lnlims.append(limit2_lnlim)
limit3_lnlims.append(limit3_lnlim)
#Reformatting the user input from GOULDS-PRINCETON to 'GOULDS' and 'PRINCETON'
input_split = user_input.split('-', 1)
st_ln1 = input_split[0]
zst_ln1 = input_split[1]
st_ln2 = st_ln1.upper()
zst_ln2 = zst_ln1.upper()
st_ln3 = "'" + str(st_ln2) + "'"
zst_ln3 = "'" + str(zst_ln2) + "'"
#Receiving analog values from user
print ('\n\t Found! \n')
print ('Enter the Specified Emergency Rating (A) for 110% for 7 minutes: ')
limit1_input = input()
print ('Enter the Specified Emergency Rating (A) for 120% for 7 minutes: ')
limit2_input = input()
print ('Enter the Specified Emergency Rating (A) for 130% for 5 minutes: ')
limit3_input = input()
Whenever I print the row_index it prints the initialized value of 0.
i = 0
row_index = 0
for i in range(len(st_lns)):
if st_ln3 == st_lns[i] and zst_ln3 == zst_lns[i]:
row_index = i
print(row_index)
limit1_input = limit1_lnlims[row_index]
limit2_input = limit2_lnlims[row_index]
limit3_input = limit3_lnlims[row_index]
csv_list = []
csv_list.append(record_lnlims)
csv_list.append(id_cos)
csv_list.append(id_dvs)
csv_list.append(id_lines)
csv_list.append(st_lns)
csv_list.append(zst_lns)
csv_list.append(id_lnlims)
csv_list.append(limit1_lnlims)
csv_list.append(limit2_lnlims)
csv_list.append(limit3_lnlims)
#Editing the csv file copy to implement new analog values
with open('lnlim_import_c.csv', 'w') as edit:
for x in zip(csv_list):
edit.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\n".format(x))