I should preface this by saying that I am not a pro, so I apologize if I am missing something really obvious, but I want to be able to open a bunch of csvs in a directory to perform the same operation on them. I've been able to do this with other files (like txt files, to parse them, for example), but not with the "with open as f" structure I am using below
I have code where if I specify the file I want to open (like this):
import csv
numlist = []
rangelist = []
designlist = []
with open("full file name", "r+") as f:
reader = csv.reader(f, delimiter="^")
for line in f:
numlist.append(line)
numlist = numlist[1:] # get rid of first line (header)
for item in numlist:
if item[0] == 'D':#evaluate if first character in item is "D"
designlist.append(item)
with open("design" + "test" + ".csv", "w") as f:
for item in designlist:
f.writelines(item)#('%s\n' % item)
It works fine, it prints the CSV exactly how I want it to. But when I try to do it for all folders in a file (like this):
import csv
import os
numlist = []
rangelist = []
designlist = []
directory_in_str = 'my directory'
directory = os.fsencode(directory_in_str)
for file in os.listdir(directory):
filename = os.fsdecode(file)
full_name = directory_in_str + filename
with open(full_name, "r+") as f:
reader = csv.reader(f, delimiter="^")
for line in f:
numlist.append(line)
numlist = numlist[1:] # get rid of first line (header)
for item in numlist:
if item[0] == 'D':#evaluate if first character in item is "D"
designlist.append(item)
with open("design" + filename + ".csv", "w") as f:
for item in designlist:
f.writelines(item)#('%s\n' % item)
I get the following error:
Traceback (most recent call last):
File "the python file", line 16, in <module>
for line in f:
File "root for where python is on my computer\Python36\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 3223: character maps to <undefined>
I've checked to make sure the files are valid, that the "type" of my variables are strings, but it seems like it isn't accepting the output of my loop.
I've used variants of that same loop to cycle through files in a directory before, and it works fine, but haven't tried it in a "with open as f" statement before. Is there something about that structure that I am missing?