I have a python program that runs a certain experiment on an artificial deck of cards. I want to save average the number of trials it took to get a certain pattern to the csv file, but I having trouble getting the program to write to a specific cvs file who's directory I've specified. The relevant code is shown below:
row = [str(n), str(limit), str(np.mean(trial_time_list)), str(max(trial_time_list)), str(np.std(trial_time_list))]
with open("D:\Documents\python projects\\results.csv", "a") as csvFile:
writer = csv.writer(csvFile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(row)
csvFile.close()
The code runs without any errors, but when I check the csv files, no new data is written onto it. Is it because I'm not running IDLE with admin permissions?
I was unable to reproduce your issue so I suspect there's something wrong with your environment. I suggest enforcing your assumptions:
import csv
import os
import numpy as np
# something about n, limit, and trial_time_list
row = [str(x) for x in (n, limit, np.mean(trial_time_list), max(trial_time_list), np.std(trial_time_list)]
path = r'D:\Documents\python projects'
os.makedirs(path, exist_ok=True)
file = os.path.join(path, 'results.csv')
with open(file, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(row)
Related
My company recently purchased a machine and I'm trying to find a way to store its data in a our data base, but first I need to clean up the CSV or Select Certain cells to write into a new csv. I'm currently Using Python 3.9XX
I need to extract the fallowing items for from this file. Serial number(Highlighted Yellow),Start time,End time, Pass-step,fail-steps, and Test Results.
If I can manage to select one cell it will try to do the rest on my own but im currently stuck trying to select the serial number and then writing into a new csv .
DATA FROM CSV
import csv
# read CSV
csvFile = r"C:\Users\Hunter\Documents\Programing\Python\Measu Dev\11.csv"
f=open(csvFile,'rt')
myReader = csv.reader(f)
Headers = ['SerialNo','PartNo','Startime','Endtime','TabPassed','TabFailed','TestResult']
Serialno = []
with open( 'Processed.csv', 'w', encoding='utf-8', newline='') as csvfile:
writer=csv.writer(csvfile)
writer.writerow(Headers)
writer.writerow(SerialNo)
RESULT
This is my ending result, I want to be able to store the serial number under its header 'SerialNo' but nothing seems to work on my end. I'm pretty new to this any help will be appreciated it.
thank you guys.
I have tried this over and over many times and it still will not work. All that I need to do is to read a csv file, but my program will not do it. I have tried the pandas.read_csv() and it just says it does not have that attribute:
My error message
I have also tried importing csv and reading it that way but it refuses to admit that there is a file there:
My other error message
And here is my code for pandas:
pd.read_csv('data.csv')
And my code for the rest:
with open('data.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
row = next(csv_reader)
print(row)
And last but not least, here is the proof that the file exists: Proof
Do not create modules using library names. Rename pandas.py. Try again.
df= pd.read_csv('data.csv')
I have a CSV file with multiple headers such as Name, CPU, Memory, OS, etc. I want to narrow the CSV list by choosing specific parameters for the headers. For example, if CPU == 2 and Memory == 4GB and OS == Windows, print out the Name.
I'm using python 2.7.
CSV file:
Name,CPU,Memory,OS
server1,2,4gb,windows
server2,4,2gb,linux
server3,8,4gb,linux
server4,4,2gb,windows
server5,2,4gb,windows
My code:
import csv
with open('test.csv', 'rb') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
print(line[1])
The only thing I can do is read the CPU column. I tried to use if statements but wouldn't work. For example, if CPU == 2, print doesn't work.
Thanks to #gboffi, I got the narrowing portion of the code below:
import csv
with open('test.csv', 'rb') as csv_file:
dict_reader = csv.DictReader(csv_file)
for d in dict_reader:
if d['CPU'] == '2' and d['OS'] == 'windows' and d['Memory'] == '4gb':
print(d['Name'])
Now the question is, how can I make a user enter the parameters instead of hardcoding them? Also, how can I export the results into a new CSV with all the parameters with the headers?
To answer just your original question, here's a good way to do it in Python 2 (note that namedtuple wasn't added to the collections module until Python 2.6):
from __future__ import print_function
from collections import namedtuple
import csv
input_filename = 'test_fields.csv'
with open(input_filename, 'rb') as csv_file:
csvreader = csv.reader(csv_file)
fieldnames = next(csvreader) # Read header row.
Record = namedtuple('Record', fieldnames) # Define fieldnames of a record.
for row in map(Record._make, csvreader):
if row.CPU == '2' and row.OS == 'windows' and row.Memory == '4gb':
print(row.Name)
Sorry, I don't really understand your follow-on questions about making a user enter the parameters and exporting the results to a new CSV — so suggest you ask a separate question about just those aspects (only after reading the csv module's documentation to see if you can figure it out yourself (especially the part about writing CSV files).
my code goes as follows:
import csv
with open('Remarks_Drug.csv', newline='', encoding ='utf-8') as myFile:
reader = csv.reader(myFile)
for row in reader:
product = row[0].lower()
filename = row[1]
product_patterns = ', '.join([i.split("+")[0].strip() for i in product.split(",")])
print(product_patterns, filename)
which outputs as below: (where film-coated tab should be one column and the filename should be another column)
film-coated tablet RECD outcome AUBAGIO IAIN-21 AoR.txt
solution for injection 093 Acceptance NO Safety profil.txt
I want to output this to a csv file with one column as product_patterns and another as filename. I wrote the below code but only the last row gets appended. Can anyone please help me with the looping here. The code I wrote is:
with open ('drug_output.csv', 'a') as csvfile:
fieldnames = ['product_patterns', 'filename']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'product_patterns':product_patterns, 'filename':filename})
enter image description here
Depending on the environment that you can use, it might be more practical to use more dedicated programs to solve your problem.
Especially the pandas package seems useful in your case.
Then you can load the csv using:
import pandas as pd
df=pd.read_csv(file_path)
After doing the necessary manipulations, you can save it again with
df.to_csv(file_path)
This will save you a lot of issues that typically occur when parsing line by line, and it should also increase performance a bit. Pandas is a pretty good package to learn anyway if you need to do some data manipulation.
i just started using python
and i've been trying to read a file
the code doesn't return any errors but it shows this message on the
debugging screen
<_csv.reader object at 0x035022B0>
i searched but i couldn't find any answers
i even tried other sample codes to see if the problem is in my writing
but it returned the same message
*Note = the file is in the same directory of the project
this is my code
import csv
with open('nora.csv', mode='r') as CSVFile:
read = csv.reader(CSVFile, delimiter =",")
print(read)
CSVFile.close()
thank you for your help in advance
Try using pandas to read csv files - it will place the csv information in a dataframe for you.
import pandas as pd
#Place the csv data into a DataFrame
file = pd.read_csv("/path/to/file.csv")
print file
i still don't know what that message meant
but i added a for loop to read instead of just adding print
and it worked
import csv
with open('players.csv', mode='r') as CSVFile:
read = csv.reader(CSVFile, delimiter =",")
for row in read :
print(row)
CSVFile.close()
thank you guys for helping <3 <3