filtering images named in a csv - python

I have a csv file with a list of image names and I want to filter the corresponding images into a new folder. Here is what I hoped could work but it doesn't. I get no error message, so I guess it iterates through the for loops but never returns a True at the if-section but I can't figure out why.
I already tried out str() but it still doesn't work.
Any ideas?
Thank you!
with open(csvFilePath, 'r', encoding='utf-8') as inp:
# run through every row of the file
for row in csv.reader(inp):
# search for filename in folder
for file in os.walk(imgFilePath):
if file == row[column]:
shutil.copy2(file, newImgPath)

Found a solution:
Little bit different approach. First we generate a list with all items of the certain column we are interested in. Then we check if the filenames are listed in the list. If True we copy the file to the new folder.
import os
import shutil
import csv
def test(csvFilePath, imgFilePath, newImgPath):
img_list = []
with open(csvFilePath, "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter = ',')
for rows in csv_reader:
img_list.append(rows[0])
for root, dirs, files in os.walk(imgFilePath):
for file in files:
if file in img_list:
shutil.copy(os.path.join(root,file), newImgPath)

Related

How to replace the header of all CSV files in a directory?

I have a folder of CSV files, and I need to simple replace the current header (first row), of the csv, with a different header. As an example, ever CSV has: A, B, C, D, E as the first first row header, but I need to be able to change that to whatever I want; i.e., Apple, Orange, Lemon, Pear, Peach || or, || 1, j, er, fd, j5
All the data in each CSV needs to be retained besides the header, and the replacement header will make all headers of all CSVs in the folder identical, per what is indicated in the code.
import shutil
import glob
files = glob.glob("/home/robert/Testing/D1/*.csv")
for i in range(len(files)):
from_file = open(files[i])
to_file = open(files[i], mode="w")
to_file.write("id,t,s,p,date,e")
shutil.copyfileobj(from_file, to_file)
I used this code, however, it deleted all of the other data in the CSV files, which I needed to keep, and only left/created the headers
from glob import glob
from pathlib import Path
def update_folder(folder: Path):
for fname in folder.glob('*.csv'):
with open(fname) as fin:
lines = fin.readlines() # element 0 is A,B,C...
lines[0] = 'Apple,Orange,Lemon\n'
with open(fname, 'w') as fout:
fout.write(''.join(readlines))
I would suggest using the Python's tempfile module to create a temporary file with the changes in it and then, after they're made, it can simply be renamed to replaced the original file. I would also using its csv module to read the original and write the updated version because it fast, debugged, and can handle many varieties of CSV.
Using the combination make the task relatively easy:
import csv
import os
from pathlib import Path
from tempfile import NamedTemporaryFile
CSV_FOLDER = Path('/home/robert/Testing/D1')
NEW_HEADER = 'id,t,s,p,date,e'.split(',')
for filepath in CSV_FOLDER.glob('*.csv'):
with open(filepath, 'r', newline='') as csv_file, \
NamedTemporaryFile('w', newline='', dir=filepath.parent, delete=False) \
as tmp_file:
reader = csv.reader(csv_file)
writer =csv.writer(tmp_file)
next(reader) # Skip header.
writer.writerow(NEW_HEADER) # Replacement.
writer.writerows(reader) # Copy remaining rows of original file.
os.replace(tmp_file.name, filepath) # Replace original file with updated version.
print('CSV files updated')

Read a file from a folder and extract a specific key from the file and save as in CSV file

I'm new to Python and the task I am performing is to extract a specific key value from a list of .iris ( which contains the list of nested dictionary format) files in a specific directory.
I wanted to extract the specific value and save it as a new .csv file and repeat it for all other files.
Below is my sample of .iris file from which I should extract only for the these keys ('uid','enabled','login','name').
{"streamType":"user",
"uid":17182,
"enabled":true,
"login":"xyz",
"name":"abcdef",
"comment":"",
"authSms":"",
"email":"",
"phone":"",
"location":"",
"extraLdapOu":"",
"mand":997,
"global":{
"userAccount":"View",
"uid":"",
"retention":"No",
"enabled":"",
"messages":"Change"},
"grants":[{"mand":997,"role":1051,"passOnToSubMand":true}],
I am trying to convert the .iris file to .json and reading the files one by, but unfortunately, I am not getting the exact output as desired.
Please, could anyone help me?
My code (added from comments):
import os
import csv
path = ''
os.chdir(path)
# Read iris File
def read_iris_file(file_path):
with open(file_path, 'r') as f:
print(f.read())
# iterate through all files
for file in os.listdir():
# Check whether file is in iris format or not
if file.endswith(".iris"):
file_path = f"{path}\{file}"
# call read iris file function
print(read_iris_file(file_path))
Your files contain data in JSON format, so we can use built-in json module to parse it. To iterate over files with certain extension you can use pathlib.glob() with next pattern "*.iris". Then we can use csv.DictWriter() and pass "ignore" to extrasaction argument which will make DictWriter ignore keys which we don't need and write only those which we passed to fieldnames argument.
Code:
import csv
import json
from pathlib import Path
path = Path(r"path/to/folder")
keys = "uid", "enabled", "login", "name"
with open(path / "result.csv", "w", newline="") as out_f:
writer = csv.DictWriter(out_f, fieldnames=keys, extrasaction='ignore')
writer.writeheader()
for file in path.glob("*.iris"):
with open(file) as inp_f:
data = json.load(inp_f)
writer.writerow(data)
Try the below (the key point here is loading the iris file using ast)
import ast
fields = ('uid','enabled','login','name')
with open('my.iris') as f1:
data = ast.literal_eval(f1.read())
with open('my.csv','w') as f2:
f2.write(','.join(fields) + '\n')
f2.write(','.join(data[f] for f in fields) + '\n')
my.csv
uid,enabled,login,name
17182,true,xyz,abcdef

Get values from .exe (converted from .py) entry field and use these values in another .py file

I have a GUI.exe (converted from a .py file) that has entry boxes.
I want to get the values that was entered in entry boxes from this GUI.exe and use it in a Python file (.py).
Is that possible? Can anyone help me in this?
You could create a csv by doing
import csv
with open('GUI_data.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(all_data) #all_data should be list of all data
With the other file you could do this to load data out as a list
with open('GUI_data.csv', newline='') as file:
file_data = csv.reader(file, delimiter=',')
transfered_data = next(file_data) # transfered_data now holds the info as a list

How can access random .csv file on my computer for further manipulations?

I am only at the beginning of my python programming way.
f = csv.reader(open('andr.csv'), delimiter=',')
andr=[]
for row in f:
This is for specific .csv file in my comp. I want to INPUT random file
file=print(input('File to analyze: '))
f = csv.reader(open(file), delimiter=',')
andr=[]
for row in f:
This obviously doesn't work. As I've already told, I am just starting to study python and it is probably very easy, but I am stuck here. I appreciate any help.
This is how basic prompts and csv reader work
import csv
while True:
filename = input('File to analyze: ')
try:
with open(filename, endline='') as fp:
for row in csv.reader(fp):
print(row)
break
except Exception as e:
print("Invalid file {} ({}), try again.".format(filename, e))
You can use glob module to get any filenames with specific extension(.csv) from any directory.
Then by using Random module you can randomly select any one file,
Please try this code,
import random
import glob
#total_files contains all CSV file names.
total_files = glob.glob(('*.csv'))
#randomly selecting one csv file.
file = random.choice(filee)
f = csv.reader(open(file), delimiter=',')
andr=[]
for row in f:
Please let me know in terms of any queries.

Skip header when writing to an open CSV

I am compiling a load of CSVs into one. The first CSV contains the headers, which I am opening in write mode (maincsv). I am then making a list of all the others which live in a different folder and attempting to append them to the main one.
It works, however it just writes over the headings. I just want to start appending from line 2. I'm sure it's pretty simple but all the next(), etc. things I try just throw errors. The headings and data are aligned if that helps.
import os, csv
maincsv = open(r"C:\Data\OSdata\codepo_gb\CodepointUK.csv", 'w', newline='')
maincsvwriter = csv.writer(maincsv)
curdir = os.chdir(r"C:\Data\OSdata\codepo_gb\Data\CSV")
csvlist = os.listdir()
csvfiles = []
for file in csvlist:
path = os.path.abspath(file)
csvfiles.append(path)
for incsv in csvfiles:
opencsv = open(incsv)
csvreader = csv.reader(opencsv)
for row in csvreader:
maincsvwriter.writerow(row)
maincsv.close()
To simplify things I have the code load all the files in the directory the python code is run in. This will get the first line of the first .csv file and use it as the header.
import os
count=0
collection=open('collection.csv', 'a')
files=[f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
if ('.csv' in f):
solecsv=open(f,'r')
if count==0:
# assuming header is 1 line
header=solecsv.readline()
collection.write(header)
for x in solecsv:
if not (header in x):
collection.write(x)
collection.close()

Categories