This question was answered/resolved in the comments section.
I am a noob to Python and I wrote the code below thinking it would copy all the rows with "NY" as the state in the state field/column to a new csv file called "Output.csv".
import csv
f = open(r'C:\Users..\input.csv', 'r')
reader = csv.DictReader(f, delimiter=',')
output = open("C:...\Output.csv",'w')
fieldnames = ['firstScan', 'FinalScan', 'City', 'State', 'cld', 'daydiff']
writer = csv.DictWriter(output, fieldnames=fieldnames, delimiter=',')
for row in reader:
if row['State'] == 'NY':
writer.writerow(row)
Everything runs fine but the output csv is completely blank. The first tab is named "Output" but the sheet is blank. If I have it output to txt, that is blank as well. Any suggestions?
Try this instead:
import csv
with open('C:/Users/felasniper/Desktop/input.csv') as f:
reader = csv.DictReader(f, delimiter=',')
output = open("C:/Users/felasniper/Desktop/Output.csv", 'w')
fieldnames = ['firstScan', 'FinalScan', 'City', 'State', 'cld', 'daydiff']
writer = csv.DictWriter(output, fieldnames=fieldnames, delimiter=',')
for row in reader:
if row['State'] == 'NY':
writer.writerow(row)
Related
I have a csv file with three lines and three columns here.
This is the csv file
At first I want to print all the lines.
Subsequently, for each of them, program check whether it is written in the second field(index 1) USA. If so, program will take the price from the third field and multiply it by two.
Now I need to rewrite this doubled price instead of 2000 (in line with the USA)
import csv
with open('countries.csv', 'r') as source:
reader = csv.reader(source)
writer = csv.writer(source)
for line in reader:
print(*line, sep=';')
with open('countries.csv', 'r') as source:
reader = csv.reader(source)
for line in reader:
if line[2] == "USA":
actual_price = int(line[2])
print(actual_price)
new_price = int(actual_price) * 2
print(new_price)
Someone has already advised me to use the creation of a new file.
But this causes problems when I want to work with the data in the file first.
import csv
import os
with open('countries.csv', mode='r') as oldfile, open(
'countries.tmp', mode='w', newline='') as newfile:
# define a reader and a writer
reader = csv.reader(oldfile, delimiter=';', quotechar='"')
writer = csv.writer(newfile, delimiter=';', quotechar='"',
quoting=csv.QUOTE_MINIMAL)
for line in reader:
print(*line, sep=';')
# copy everything changing the third field
for line in reader:
if line[2] == "USA":
actual_price = int(line[2])
print(actual_price)
new_price = int(actual_price) * 2
print(new_price)
for row in reader:
writer.writerow([row[0], row[1], ,new_price])
# ok, time to rename the file
os.replace('countries.tmp', 'countries.csv')
Thank you for answer
You are changing new_price at every iteration of your for loop. You should therefore be writing the row within the loop where you change the value:
with open('countries.csv', mode='r') as oldfile, open('countries.tmp', mode='w', newline='') as newfile:
reader = csv.reader(oldfile, delimiter=';', quotechar='"')
writer = csv.writer(newfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in reader:
price = int(row[2])
if row[1] == "USA":
price = price*2
writer.writerow([row[0], row[1], price])
os.replace('countries.tmp', 'countries.csv')
def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
//with open('DADSA 2019-20 CWK A DATA PETS.csv', 'wb') as csv_file:
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
# reader = csv.reader(csv_file)
writer = csv.writer(csv_file)
for row in csv.reader(csv_file):
if row[0] == newRow[0]:
writer.writerow(newRow)
I am getting this error "for row in csv.reader(csv_file):
io.UnsupportedOperation: not readable" I dont really understand it what it means.
You have a typo, you should use newRow, not csvFile, fixed code below:
def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
#with open('DADSA 2019-20 CWK A DATA PETS.csv', 'wb') as csv_file:
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
# reader = csv.reader(csv_file)
writer = csv.writer(csv_file)
for row in csv.reader(newRow):
if row[0] == newRow[0]:
writer.writerow(newRow)
You can't read from a file opened in a mode. That's for appending to the file. You could use a+ mode, but you'll need to seek to the beginning of the file first.
But if you only want to add a new row, you don't need to read from the file first. Just open the file and write the new row.
def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(newRow)
I am trying to loop multiple arrays and write them into csv file with under right filednames.
Here is the working code but is is bad approach and not professional:
# zip arrays
rows = zip(en,pl,tr,de)
# Write to CSV
with open('translations.csv', mode='w', newline='', encoding="utf-8") as csv_file:
fieldnames = ['English', 'Polish', 'Turkish', 'German']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
info = inspect.getmembers(row)
english = row[0]
polish = row[1]
turkish = row[2]
german = row[3]
writer.writerow({'English': english, 'Polish':polish, 'Turkish':turkish, 'German':german})
I tried to change the code and it is really messed up:
# zip arrays
rows = zip(en,pl,tr,de)
# Write to CSV
with open('translations.csv', mode='w', newline='', encoding="utf-8") as csv_file:
fieldnames = ['English', 'Polish', 'Turkish', 'German']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
for i in range(len(fieldnames)):
writer.writerow({fieldnames[i]:row[i]})
So every translation should under right filedname. Here the result of this code:
English,Polish,Turkish,German
Test Name,,,
,Test Name,,
,,Test Name,
,,,Test Name
Nazwa testu,,,
,Nazwa testu,,
,,Nazwa testu,
,,,Nazwa testu
test Adı,,,
,test Adı,,
,,test Adı,
,,,test Adı
Testname,,,
,Testname,,
,,Testname,
,,,Testname
It is really messed up. Please help me to solve this problem. Thank you!
You probably need this. Instead of
for i in range(len(fieldnames)):
writer.writerow({fieldnames[i]:row[i]})
Use:
writer.writerow(dict(zip(fieldnames, row)))
I have data that look like below
id,Stage1Pow1,Stage1Pow2,Stage2Pow1,Stage2Pow2
A,1.0,1.5,1.1,1.4
B,0.9,1.2,0.9,1.1
C,1.0,1.0,0.8,0.8
how do I convert them into below format, in order to store and further analyze by stage.
id,StageNo,Pow1,Pow2
A,s1,1.0,1.5
A,s2,1.1,1.4
B,s1,0.9,1.2
B,s2,0.9,1.1
C,s1,1.0,1.0
C,s2,0.8,0.8
I'm very new to Python.
Let 'raw' be your input file name, and 'output' be your output file name.
import csv
data = []
with open('raw', 'r') as f:
csvreader = csv.reader(f, delimiter=',')
for row in csvreader:
data.append(row)
with open('output', 'w') as f:
csvwriter = csv.writer(f)
csvwriter.writerow(['id', 'StageNo', 'Pow1', 'Pow2'])
for row in data[1:]:
csvwriter.writerow([row[0], 's1', row[1], row[2]])
csvwriter.writerow([row[0], 's2', row[3], row[4]])
more info on https://docs.python.org/2/library/csv.html
You can use csv modules csv.DictReader() and csv.DictWriter()
import csv
with open('input.csv','r') as file:
reader = csv.DictReader(file)
with open('output.csv','w') as file_output:
writer = csv.DictWriter(file_output,fieldnames=['id','StageNo','Pow1','Pow2'])
writer.writeheader()
for line in reader:
line_split1 = {'id':line['id'],'StageNo':'s1','Pow1':line['Stage1Pow1'],'Pow2':line['Stage1Pow2']}
line_split2 = {'id':line['id'],'StageNo':'s2','Pow1':line['Stage2Pow1'],'Pow2':line['Stage2Pow2']}
writer.writerows([line_split1,line_split2])
Output:
id,StageNo,Pow1,Pow2
A,s1,1.0,1.5
A,s2,1.1,1.4
B,s1,0.9,1.2
B,s2,0.9,1.1
C,s1,1.0,1.0
C,s2,0.8,0.8
I have hundreds of .csv files with 40 rows and 34 columns each. I want to add a column at position 26 and column 26-34 should shift to make space for the new one. First row of the file is empty and second row has the titles and rest have the values. The new column should have a title in row two and rest of the rows can be zero.
Please help me with this code in python.
import csv
infilename = r'C:\Users\Sulabh Kumra\Desktop\input.csv'
outfilename = r'C:\Users\Sulabh Kumra\Desktop\output.csv'
with open(infilename, 'rb') as fp_in, open(outfilename, 'wb') as fp_out:
reader = csv.reader(fp_in, delimiter=",")
headers = next(reader) # read first row
writer = csv.writer(fp_out, delimiter=",")
writer.writerow(headers)
for row in reader:
row.append(row[2])
writer.writerow(row)
Inserting into a python list is pretty easy: some_list[2:2] = ['stuff','to','insert']
So your code would look like the following:
import csv
infilename = r'C:\Users\Sulabh Kumra\Desktop\input.csv'
outfilename = r'C:\Users\Sulabh Kumra\Desktop\output.csv'
with open(infilename, 'rb') as fp_in, open(outfilename, 'wb') as fp_out:
reader = csv.reader(fp_in, delimiter=",")
writer = csv.writer(fp_out, delimiter=",")
blank_line = next(reader)
writer.writerow(blank_line)
headers = next(reader) # read title row
headers[26:26] = ['New Label']
writer.writerow(headers)
for row in reader:
row[26:26] = [0]
writer.writerow(row)