Make a new list from CSV - python

So, I've search for a method to show a certain csv field based on input, and I've try to apply the code for my program. But the problem is I want to get a certain item in csv and make a new list from certain index.
I have csv file like this:
code,place,name1,name2,name3,name4
001,Home,Laura,Susan,Ernest,Toby
002,Office,Jack,Rachel,Victor,Wanda
003,Shop,Paulo,Roman,Brad,Natali
004,Other,Charles,Matthew,Justin,Bono
at first I have this code, and it works show all the row:
import csv
number = input('Enter number to find\n')
csv_file = csv.reader(open('residence.csv', 'r'), delimiter=",")
for row in csv_file:
if number == row[0]:
print (row)
**input : 001
**result : [001, Home, Laura, Susan, Ernest, Toby]
then, I try to make a certain row in the result to add the items to a new list. But it didn't work. Here's the code:
import csv
res = []
y = 2
number = input('Enter number to find\n')
csv_file = csv.reader(open('residence.csv', 'r'), delimiter=",")
for row in csv_file:
if number == row[0]:
while y <= 5:
res.append(str(row[y]))
y = y+1
print (res)
**input : 001
**expected result : [Laura, Susan, Ernest, Toby]
I want to make a new list that contains row name1, name2, name3, and name4, and then I want to print the list. But I guess the loop is wrongly placed or I missed something.

There are a couple of things you could fix in your code.
You are not skipping the header line when iterating through the rows. This means you will not always match an actual row number.
Your y variable is not re-initialized. It would be more idiomatic to use a for loop instead of a while anyhow.
If more than one row match, it will break (see 2.). If you know you will never have more than one match, you should break after you append the values to the list.
Your file is never closed. Also it should be opened with newline='' (see csv module docs)
Lastly, you match the actual string ('001'), vs. an integer (1), which could be the source of confusion when entering the input.
An updated version:
import csv
res = []
number = input('Enter number to find\n')
with open('residence.csv', newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
next(csv_reader) # Skip header line
for row in csv_reader:
if number == row[0]:
for i in range(2, 6):
res.append(str(row[i]))
break
print(res)

Related

Compare 2 csv files and check for first 2 columns, if it matches ask the user to decide to override or not and then proceed to next row

I have a use case where I have 2 CSV files with some rows in each CSV file, and they have three columns each. Compare the 2 csv files for first 2 columns and if it matches then ask the user input if he wants to override the row in the first csv file with the values from second csv file, if not abort the operation.
First time when I run the python code it should update the csv file with the new values from the 2nd CSV file to first csv file, but for consecutive runs of my python code I have to check if first 2 columns match and ask the user to decide if he needs to override the values or not, since now the first csv file will have rows from first csv file.
My code:
import csv
import sys
def csv_file_copy():
csv_file = input("Enter the CSV file needs to be updated ")
csv_file_cp = input("Enter the csv file from where the data needs to be copied ")
csvfile = open(csv_file_cp, 'r',encoding="utf-8-sig")
reader = csv.reader(csvfile)
csv_file_orig = open(csv_file, 'r',encoding="utf-8-sig")
reader2 = csv.reader(csv_file_orig)
res = []
for row in reader:
print("This is row", row)
for row2 in reader2:
print("This is row2", row2)
if (row2[0] == row[0] and row2[1] == row[1]):
user_input = input("Store type and store number already exists in the csv file, continue? y/n ").lower()
if user_input == "y":
res.append(row)
elif user_input == "n":
print("Aborting operation")
sys.exit(1)
else:
res.append(row2)
res.append(row)
continue
print (reader)
with open(csv_file, 'w') as csv_file1:
writer = csv.writer(csv_file1, delimiter=',')
for row in res:
writer.writerow(row)
csv_file_copy()
When the code is executed second time against the same 2 files the second for loop runs only once thereby matching only one value but there are about 10 values that is matching which doesn't work for me.
If the csv_file_orig is not too big (or your available memory too low) then you may store the whole contents into a list.
Instead of
reader2 = csv.reader(csv_file_orig)
You'll use
csv_file_orig_lines = list(csv.reader(csv_file_orig))
Afterwards you may iterate through csv_file_orig_lines list as many times as you want.

Is it possible to remove a specific row from csv file by giving one element of the row? Now index but the element itself

I have made a csv file in python and then have imported it to python. Now I want to write a for loop and if statement to remove one specific row from the file. The same way as I print the specific row by using specific element from the row.
Here is how a get access to a row:
data=[]
with open("platsbiljet.csv") as csvfile:
reader=csv.reader(csvfile)
for row in reader:
data.append(row)
print(data)
lookup = input("Please enter your seat number:")
colx= [s[4] for s in data]
print(colx)
if lookup in colx:
for k in range(0, len(colx)):
if colx[k]==lookup:
print(data[k])
else:
print("No seat with that number!")
Since all your rows are read and stored in a list.
You can use the following to delete an element from list (More info here):
del data[k]
Then, you can save the CSV:
with open('your_file.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(data)

Reformat .csv in python: count commas in a row and insert line break after specific number of commas

I'm new to python and looking for a script that reformats a .csv file. So in my .csv files there are rows which are not formatted correctly. It does look similar to this:
id,author,text,date,id,author,
text,date
id,author,text,date
id,author,text,date
It's supposed to have "id,author,text,date" on each line. So my idea was to count the commas in each row and when a specific number is achieved (in this example 4) it will insert the remainder at the beginning of the next row. What I got is the following which counts the commas in one row:
import csv
with open("test.csv") as f:
r = csv.reader(f) # create rows split on commas
for row in r:
com_count = 0
com_count += len(row)
print(com_count)
Thanks for your help!
We're going to build a generator that yields entries and then build the new rows from that
with open('oldfile.csv', newline='') as old:
r = csv.reader(old)
num_cols = int(input("How many columns: "))
entry_generator = (entry for row in r for entry in row)
with open('newfile.csv', 'w+', newline='') as newfile:
w = csv.writer(newfile)
while True:
try:
w.writerow([next(entry_generator) for _ in range(num_cols)])
except StopIteration:
break
This will not work if you have a row that is missing entries.
If you want to handle getting the column width programmatically, you can either wrap this in a function that takes a width as input, or use the first row of the csv as a canonical length

Iterating through particular rows in a csvFile in Python

I have a programming assignment that include csvfiles. So far, I only have a issue with obtaining values from specific rows only, which are the rows that the user wants to look up.
When I got frustrated I just appended each column to a separate list, which is very slow (when the list is printed for test) because each column has hundreds of values.
Question:
The desired rows are the rows whose index[0] == user_input. How can I obtain these particular rows only and ignore the others?
This should give you an idea:
import csv
with open('file.csv', 'rb') as f:
reader = csv.reader(f, delimiter=',')
user_rows = filter(lambda row: row[0] == user_input, reader)
Python has the module csv
import csv
rows=[]
for row in csv.reader(open('a.csv','r'),delimiter=','):
if(row[0]==user_input):
rows.append(row)
def filter_csv_by_prefix (csv_path, prefix):
with open (csv_path, 'r') as f:
return tuple (filter (lambda line : line.split(',')[0] == prefix, f.readlines ()))
for line in filter_csv_by_prefix ('your_csv_file', 'your_prefix'):
print (line)

CSV code not looping

I'm trying to make a small Python script to speed up things at work and have a small script kind of working, but it's not working as I want it to. Here's the current code:
import re
import csv
#import pdb
#pdb.set_trace()
# Variables
newStock = "newStock.csv" #csv file with list of new stock
allActive = "allActive.csv" #csv file with list of all active
skusToCheck= []
totalNewProducts = 0
i = 0
# Program Start - Open first csv
a = open(newStock)
csv_f = csv.reader(a)
# Copy each row into array thingy
for row in csv_f:
skusToCheck.append(row[0])
# Get length of array
totalNewProducts = len(skusToCheck)
# Open second csv
b = open(allActive)
csv_f = csv.reader(b)
# Open blank csv file to write to
csvWriter = csv.writer(open('writeToMe.csv', 'w'), delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
# Check first value in first row,first file against each entry in 2nd row in second file
with open(allActive, 'rt') as b:
reader = csv.reader(b, delimiter=",")
for row in reader:
if skusToCheck[i] == row[1]:
print(skusToCheck[i]) # output to screen for debugging
print(row) # debugging
csvWriter.writerow(row) #write matching row to new file
i += 1 # increment where we are in the first file
Pseudo code would be:
Open file one and store all values from column one in skusToCheck
Check this value against values in column 2 in file 2
If it finds a match, (once I have this working, i want it to look for partial matches too) copy the row to file 3
If not move onto the next value in skusToCheck and repeat
I can't seem to get lines 33 - 40 to loop. It will check the first value and find a match in the second file, but won't move onto the next value from skusToCheck.
You need to follow the hint from jonrsharpe's first comment, i.e. modify your while loop to
# Check first value in first row,first file against each entry in 2nd row in second file
with open(allActive, 'rt') as b:
reader = csv.reader(b, delimiter=",")
for row in reader:
if len(row)>1:
for sku in skusToCheck:
if sku == row[1]:
print(sku) # output to screen for debugging
print(row) # debugging
csvWriter.writerow(row) #write matching row to new file
break
This checks if each single sku is matching for all of the rows in allActive

Categories

Resources