Python edit specific row and column of csv file - python-3.x

I have some python code here with the aim of taking user input to target a specific row of a CSV and then to overwrite it if a certain letter matches.
import csv
line_count = 0
marked_item = int(input("Enter the item number:"))
with open("items.csv", 'r') as f:
reader = csv.reader(f, delimiter=',')
title = next(reader)
print(title)
print(title[3])
lines = []
for line in reader:
if title[3] == 'a':
line_count += 1
if marked_item == line_count:
title[3] = 'b'
lines.append(line)
with open("items.csv", 'w', newline='') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(title)
writer.writerows(lines)
This code works almost the way I want it to but it is unable to edit any other row but the first. an example of the output this code is:
red,12.95,2,b #note, this change from 'a' to 'b'
blue,42.5,3,a #How can I target this row and change it?
green,40.0,1,a
the problem I then have it targeting another row like row 'blue,42.5,a'. My code is unable to target and then change the value 'a' to 'b'.

you're iterating on line and you change title. Do this:
for line in reader:
if len(line)>3 and line[3] == 'a':
line_count += 1
if marked_item == line_count:
line[3] = 'b'
lines.append(line)
and drop the title = next(reader) since you don't have a title.
full fixed code for input csvs that don't have a title line:
import csv
line_count = 0
marked_item = int(input("Enter the item number:"))
with open("items.csv", 'r') as f:
reader = csv.reader(f, delimiter=',')
lines = []
for line in reader:
if len(line)>3 and line[3] == 'a':
line_count += 1
if marked_item == line_count:
line[3] = 'b'
lines.append(line)
with open("items.csv", 'w', newline='') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(title)
writer.writerows(lines)

Related

I want to only copy list of records from csv1 file to newFile.csv whose first row item starting with letter is 'A'

This is code
I want only first item word is 'A' data stored in newFile.csv file. How i can Do?
student_databaseOne = 'firstFile.csv'
NewFile = 'NewFile.csv'
letter = 'A'
with open(student_databaseOne, "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
for item in row:
if item == 'A':
for row in reader:
with open(NewFile, "w", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(row)
This script will read the firstFile.csv row by row, and when the first item is equal to A it stores the row into NewFile.csv:
import csv
with open('firstFile.csv', 'r') as f_in, open('NewFile.csv', 'w') as f_out:
reader = csv.reader(f_in, delimiter=',', quotechar='"')
writer = csv.writer(f_out, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
for first, *rest in reader:
if first == 'A':
writer.writerow([first, *rest])

Getting an IndexError: list index out of range in line no. = 19?

import csv
with open('C:/Users/dkarar/Desktop/Mapping project/RC_Mapping.csv', 'r') as file1:
with open('C:/Users/dkarar/Desktop/Mapping project/Thinclient_mapping.csv', 'r') as file2:
with open('C:/Users/dkarar/Desktop/Mapping project/output.csv', 'w') as outfile:
writer = csv.writer(outfile)
reader1 = csv.reader(file1)
reader2 = csv.reader(file2)
for row in reader1:
if not row:
continue
for other_row in reader2:
if not other_row:
continue
# if we found a match, let's write it to the csv file with the id appended
if row[1].lower() == other_row[1].lower():
new_row = other_row
new_row.append(row[0])
writer.writerow(new_row)
continue
# reset file pointer to beginning of file
file2.seek(0)
You seem to be getting at least one row where there is a single element. That's why when accessing row[1] you get an IndexError, there's only one element in the list row.

Write a list to CSV

I need to write the following values from a list to a CSV.
my_list[0]
March,'1.800931853','1.843227636','1.861992226','1.880196477','1.907555762','1.853076585','1.770560438','1.837108443','1.867213313','1.849542335','1.913505573','2.023772836','1.899717681'
my_list[1]
July,'10.809832','10.57868801','9.571741038','8.850265221','9.154250034','9.172628705','10.35004201','9.36852657','8.689944331','10.1053826','9.18756731','8.541507801','8.054240996'
I have tried the following:
def saveData(my_list):
file_name = 'months.csv'
with open(file_name, 'w') as f:
writer = csv.writer(f)
for row in zip(my_list):
writer.writerows([row])
f.close()
The CSV should look like this with each value in a separate cell
March 1.800931853 1.843227636 1.861992226 1.880196477 1.907555762 1.853076585 1.770560438 1.837108443 1.867213313 1.849542335 1.913505573 2.023772836 1.899717681 2.152980753
This works with Python 3:
import csv
def save_data(my_list):
file_name = 'months.csv'
with open(file_name, 'w') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_NONE, delimiter=' ')
for row in my_list:
wr.writerow(map(str, row))
#example:
save_data([["March", 1.2, 1.5], ["July", 2.3, 4.5678, 9.101]])

Using Python to delete rows in a csv file that contain certain chars

I have a csv file that I'm trying to clean up. I am trying to look at the first column and delete any rows that have anything other than chars for that row in the first column (I'm working on cleaning up rows where the first column has a ^ or . for now). It seems all my attempts either do nothing or nuke the whole csv file.
Interestingly enough, I have code that can identify the problem rows and it seems to work fine
def FindProblemRows():
with open('Data.csv') as csvDataFile:
ProblemRows = []
csvReader = csv.reader(csvDataFile)
data = [row for row in csv.reader(csvDataFile)]
length = len(data)
for i in range (0,length):
if data[i][0].find('^')!=-1 or data[i][0].find('.')!=-1:
ProblemRows.append(i)
return (ProblemRows)
Below I have my latest three failed attempts. Where am I going wrong and what should I change? Which of these comes closest?
'''
def Clean():
with open("Data.csv", "w", newline='') as f:
data = list(csv.reader(f))
writer = csv.writer(f)
Problems = FindProblemRows()
data = list(csv.reader(f))
length = len(data)
for row in data:
for i in Problems:
for j in range (0, length):
if row[j] == i:
writer.writerow(row)
Problems.remove(i)
def Clean():
Problems = FindProblemRows()
with open('Data.csv') as csvDataFile:
csvReader = csv.reader(csvDataFile)
data = [row for row in csv.reader(csvDataFile)]
length = len(data)
width = len(data[0])
with open("Data.csv","r") as csvFile:
csvReader = csv.reader( csvFile )
with open("CleansedData.csv","w") as csvResult:
csvWrite = csv.writer( csvResult )
for i in Problems:
for j in range (0, length):
if data[j] == i:
del data[j]
for j in range (0, length):
csvWrite.writerow(data[j])
'''
def Clean():
with open("Data.csv", 'r') as infile , open("CleansedData.csv", 'w') as outfile:
data = [row for row in infile]
for row in infile:
for column in row:
if "^" not in data[row][0]:
if "." not in data[row][0]:
outfile.write(data[row])
Update
Now I have:
def Clean():
df = pd.read_csv('Data.csv')
df = df['^' not in df.Symbol]
df = df['.' not in df.Symbol]
but I get KeyError: True
Shouldn't that work?
You should check whether the column Symbol contains any of the characters of interest. Method contains takes a regular expression:
bad_rows = df.Symbol.str.contains('[.^]')
df_clean = df[~bad_rows]

create a new csv file in new subdirectory python 3.6

Been having trouble with using variables in a new dirpath when using with open to create a csv file.
I can create the dirpath fine along with the .csv filename and add them with os.path.join .
But when i try to use the entire correctly formatted path in the open statement it is adding an extra backslash next to the current backslashes in the string?? could not find any documentation on why this may be happening.
This is the error
FileNotFoundError: [Errno 2] No such file or directory: 'Logs\\14-05-2018\\dfg.csv'
If i simply write the following i can create the subfolders and csvs no problemo.
with open(Logs\1234\asd.csv) 'w') as csvfile:
So this is what i am getting an error in on line 43 which is the with open(csvlogPath, 'w') as csvfile: I have tried with r instead of 'w' and same problem. At a loss after two days time to ask you guys. :)
import csv, os.path
#Get and create csv filename string
logFN = input("Please name your trade.\n#")
suffix = '.csv'
csvname = logFN + suffix
#Create dir name string
t = time.strftime("%d-%m-%Y-%H%M")
dirsname = os.path.join('Logs', t)
csvlogPath = os.path.join(dirsname,csvname)
with open(csvlogPath, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['get', 'new', 'values', 'later'])
with open(csvlogPath, 'w') as f:
reader = csv.reader(f)
# read file row by row
rwsCount = 0
for row in reader:
print (row, rwsCount)
rwsCount += 1
line_number = rwsCount-1
print (line_number)
with open(csvlogPath, 'w') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
text = mycsv[line_number][0]
print (text)
you need to make the dirs in python you can use os.makedirs(path) like so:
csvlogPath = os.path.join(dirsname,csvname)
os.makedirs(os.path.dirname(csvlogPath), exist_ok=True)
with open(csvlogPath, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['get', 'new', 'values', 'later'])
With open, use 'r' to read and 'w' to write so you need to change the next to opens to:
with open(csvlogPath, 'r') as f:
reader = csv.reader(f)
# read file row by row
rwsCount = 0
for row in reader:
print (row, rwsCount)
rwsCount += 1
line_number = rwsCount-1
print (line_number)
with open(csvlogPath, 'r') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
text = mycsv[line_number][0]
test this code :
import csv, os.path
#Get and create csv filename string
import time
logFN = input("Please name your trade.\n#")
suffix = '.csv'
csvname = logFN + suffix
#Create dir name string
t = time.strftime("%d-%m-%Y-%H%M")
dirsname = os.path.join('Logs', t)
###########################
if not os.path.exists(dirsname):
os.makedirs(dirsname)
###########################
csvlogPath = os.path.join(dirsname,csvname)
with open(csvlogPath, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['get', 'new', 'values', 'later'])
with open(csvlogPath, 'r') as f:
reader = csv.reader(f)
# read file row by row
rwsCount = 0
for row in reader:
print (row, rwsCount)
rwsCount += 1
line_number = rwsCount-1
print (line_number)
with open(csvlogPath, 'r') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
text = mycsv[line_number][0]
print (text)

Resources