Deleting specific lines in csv files
import csv
with open('output.csv', 'w+') as output_file:
with open('input.csv') as input_file: #change you file name here
reader = csv.reader(input_file, delimiter = '\n')
line_index = 0 # debugging
for row in reader:
line_index += 1
line = row[0]
if 'step' in line:
output_file.write(line)
output_file.write('\n')
else:
try:
number = int(line) # you can use float, but then 30 become 30.0
if number <= 27:
output_file.write(line)
output_file.write('\n')
except:
print("Abnormal data at line %s", str(line_index))
I assume that your input file is input.csv. This program writes to new output file. The output is output.csv:
step0:
step1:
step2:
4
step3:
step4:
step5:
15
step6:
13
step7:
19
How to delete specific data from single line in CSV FILE and last line?
Use the csv
module to read and rewrite back without the last 3 columns
for file in myfiles:
rows = []
with io.open(file,"r",encoding="utf-8") as f:
reader = csv.reader(f, delimiter=",", quotechar='"')
for row in reader:
rows.append(row[:-3])
with io.open(file,"w",encoding="utf-8") as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
Remove rows from CSV file containing certain characters
I would actually not use the csv
package for this goal. This can be achieved easily using standard file reading and writing.
Try this code (I have written some comments to make it self-explanatory):
# We open the source file and get its lines
with open('input_csv_file.csv', 'r') as inp:
lines = inp.readlines()
# We open the target file in write-mode
with open('purged_csv_file.csv', 'w') as out:
# We go line by line writing in the target file
# if the original line does not include the
# strings 'py-board' or 'coffee'
for line in lines:
if not 'py-board' in line and not 'coffee' in line:
out.write(line)
Remove rows from .csv file using python
You can also do this:
lines = list()
remove= [1,2,3,4]
with open('dataset1.csv', 'r') as read_file:
reader = csv.reader(read_file)
for row_number, row in enumerate(reader, start=1):
if(row_number not in remove):
lines.append(row)
with open('new_csv.csv', 'w') as write_file:
writer = csv.writer(write_file)
writer.writerows(lines)
How to delete a row in a CSV file if a cell is empty using Python
Due to the way in which CSV reader presents rows of data, you need to know how many columns there are in the original CSV file. For example, if the CSV file content looks like this:
1,2
3,
4
Then the lists return by iterating over the reader would look like this:
['1','2']
['3','']
['4']
As you can see, the third row only has one column whereas the first and second rows have 2 columns albeit that one is (effectively) empty.
This function allows you to either specify the number of columns (if you know them before hand) or allow the function to figure it out. If not specified then it is assumed that the number of columns is the greatest number of columns found in any row.
So...
import csv
DELIMITER = ','
def valid_column(col):
try:
return float(col) != 0
except ValueError:
pass
return len(col.strip()) > 0
def fix_csv(input_file, output_file, cols=0):
if cols == 0:
with open(input_file, newline='') as indata:
cols = max(len(row) for row in csv.reader(indata, delimiter=DELIMITER))
with open(input_file, newline='') as indata, open(output_file, 'w', newline='') as outdata:
writer = csv.writer(outdata, delimiter=DELIMITER)
for row in csv.reader(indata, delimiter=DELIMITER):
if len(row) == cols:
if all(valid_column(col) for col in row):
writer.writerow(row)
fix_csv('original.csv', 'fixed.csv')
How to remove a row in a CSV file while looping through each row to run a function?
As @OneCriketeer pointed out, the only way to "modify" a file is to completely overwrite it with the modified data. To that end, I propose:
- Reading all URLs from your repository CSV into a list
- Process the list, keeping track of successful executions of your function
- Subtract your processed URLs from the original list, leaving you with unprocessed
- Write over your repository CSV with the list of unprocessed URLs
All that effectively deletes processed lines from the original.
I cannot run this, so there may be a few typos in it, but here's the general idea:
repositories = []
with open('Repositories.csv', newline='') as csv_file:
reader = csv.reader(csv_file)
repositories = list(reader)
processed = []
for repo in repositories:
print("repository: " + repo) # coming straight out of a CSV, values are always strings, no str() conversion required
print("1) Migrate repo")
print("2) Skip repo")
print("3) Exit")
a = input("Please choose an option above: ")
# ... do stuff
# ... finally:
processed.append(repo)
# Use set() to "subtract" one list from another
unprocessed = set(repositories) - set(processed)
# unprocessed is now a set, still iterable, but convert back to a list if you like
# unprocessed = list(unprocessed)
with open('Repositories.csv', 'w', newline='') as csv_file:
reader = csv.writer(csv_file)
writer.writerows(unprocessed)
How to delete only one row in CSV with python
This worked for me: you could write the contents of the csv file to a list, then edit the list in python, then write the list back to the csv file.
lines = list()
memberName = input("Please enter a member's name to be deleted.")
with open('mycsv.csv', 'r') as readFile:
reader = csv.reader(readFile)
for row in reader:
lines.append(row)
for field in row:
if field == memberName:
lines.remove(row)
with open('mycsv.csv', 'w') as writeFile:
writer = csv.writer(writeFile)
writer.writerows(lines)
How to remove a range of lines from csv file?
You can use csv library for doing this.
import csv
file=open("Database.csv",'rb')
final_file=open("Database_edited",'wb')
writer=csv.writer(final_file)
line_no=1 # for knowing the line number
for row in csv.reader(file):
if(line_no<=3 or line_no>=15):
writer.writerow(row)
line_no=line_no+1
file.close()
final_file.close()
This way Database_edited will have your required file
how to delete rows from a csv file which string in 1st column is the same of string in 1st column of another csv?
I found a solution. Since the entire file 2 had to be removed from file 1, I did the following command, which informed just the first column to be compared, and it worked:
df1.loc[pd.merge(df1, df2, on=['Genus'], how='left', indicator=True)['_merge'] == 'left_only']
Thanks for you time!
AP
Deleting a row from a CSV based on line number and shifting all lines afterwards
Try:
import pandas as pd
def remove_nth_line_csv(file_name, n):
df = pd.read_csv(file_name, header=None)
df.drop(df.index[n], inplace=True)
df.to_csv(file_name, index=False, header=False)
Remember pandas indexes from 0. Therefore, counting starts 0,1,2,3,4...n
Related Topics
PHP SQL Select Where Like Search Item with Multiple Words
Why Can't I Overload Constructors in PHP
Warning: Cannot Modify Header Information - Headers Already Sent
List of Us Time Zones for PHP to Use
Empty Values Passed to Zend Framework 2 Validators
Magento Addfieldtofilter: Two Fields, Match as Or, Not And
Pros and Cons of Interface Constants
How to Remove a Key and Its Value from an Associative Array
PHP Loop: Add a Div Around Every Three Items Syntax
PHP - Getsqlvaluestring Function
Why Is It Good Save to Save Sessions in the Database
PHP Allocate Color Without Image Resource
Combine 2-3 Transparent Png Images on Top of Each Other with PHP
Improve This PHP Bitfield Class for Settings/Permissions
Memory Optimization in PHP Array