htm. See also .
Category:Variations of root positionQ:
Python - Split CSV files by column and modify
I'm trying to modify my code to do something like this:
For every file in a folder, I want to check if the col1 and col2 are equal (using this code)
if (col1 == "yes" and col2 == "yes"):
with open("Data/something.csv") as f:
reader = csv.reader(f, delimiter=";")
for row in reader:
if (row[2] == "yes"):
row[2] = "modified"
with open("Data/something.csv", 'a') as file:
writer = csv.writer(file, delimiter=";")
writer.writerow(row)
I am working with a large number of files with 10 columns in each, and each has about 15,000 rows. I'm trying to figure out how to automate the modification and move of these files to the modified folder.
A:
To do this you need to create a dictionary that contains the filenames you want to process. You can then use os.listdir to get all of the files in a folder. You can then check the name of each file and process it appropriately.
# Initialize your file name dictionary, sort of
file_name_map = {}
for filename in os.listdir("Data/"):
# Use a dictionary comprehension to make this assignment a one liner
# It is easier to read
file_name_map[filename] = "Data/something.csv"
# Use os.path.isfile to check if you actually
Related links:
Comentários