I have a small script that reads a pipe '|' delimited csv file and then writes it to another file as comma delimited. It works fine. Code, input file and output file below:
import csv
ifile = "d:\\python\\project\\cars-original.csv"
ofile = "d:\\python\\project\\cars.csv"
with open(ifile, 'r') as f:
reader = csv.reader(f, delimiter='|')
with open(ofile, 'w', newline='') as of:
writer = csv.writer(of, delimiter=',')
for row in reader:
writer.writerow(row)
Reading|Make|Model|Type|Value
Reading 0|Toyota|Previa|distance|19.83942
Reading 1|Dodge|Intrepid|distance|31.28257
Reading,Make,Model,Type,Value
Reading 0,Toyota,Previa,distance,19.83942
Reading 1,Dodge,Intrepid,distance,31.28257
I now want to modify the script so that it can auto-read the delimiter type.
I have found several examples online, but when I run mine I get a blank file for output. No errors, just blank. Not sure what I am doing wrong.
My modified (broken) script:
import csv
ifile = "d:\\python\\projects\\cars-original.csv"
ofile = "d:\\python\\projects\\cars.csv"
with open(ifile, 'r') as f:
reader = csv.reader(f)
dialect = csv.Sniffer().sniff(f.read(1024), delimiters=',|')
with open(ofile, 'w', newline='') as of:
writer = csv.writer(of, dialect)
for row in reader:
writer.writerow(row)
In the example in Python documentation (https://docs.python.org/3.6/library/csv.html) file pointer is moved to the beginning of the file, right after the dialect detection.
with open('example.csv', newline='') as csvfile:
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)
This works:
import csv
ifile = "d:\\python\\project\\cars-original.csv"
ofile = "d:\\python\\project\\cars.csv"
with open(ifile, 'r') as f:
dialect = csv.Sniffer().sniff(f.readline(), [',', '|'])
f.seek(0)
reader = csv.reader(f, dialect)
with open(ofile, 'w', newline='') as of:
writer = csv.writer(of, delimiter=',')
for row in reader:
writer.writerow(row)
Related
I have been using this code to extract data from website and store it into a csv file. However, I would like to know how can I add an empty row between the data without using hardcode, for instance a empty row that seperate the title 'Total' and the data extracted from Total.text. (Like this picture.) This is the link I extra data "https://finance.yahoo.com/quote/0700.HK/sustainability?p=0700.HK"
with open(f'{stockname}_sustainability.csv', 'w', newline='') as csvfile:
fieldnames = ['Total','Type','Percentile','ERS','SRS','GRS','mclevel']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'Total':int(Total.text),'Type' : Type.text,'Percentile':int(Percentile[0:2]), 'ERS' : float(ERS.text),'SRS':float(SRS.text),'GRS':float(GRS.text),'mclevel':int(mclevel.text)})
import csv
with open('Book1.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
fieldnames = ['Total', 'Type', 'Percentile', 'ERS', 'SRS', 'GRS', 'mclevel']
writer.writerow("".join(fieldnames))
writer.writerow("\n")
writer.writerow({'Total':int(Total.text),'Type' : Type.text,'Percentile':int(Percentile[0:2]), 'ERS' : float(ERS.text),'SRS':float(SRS.text),'GRS':float(GRS.text),'mclevel':int(mclevel.text)})
By using write.writerow("\n") you will create one empty row.
I am trying to read a csv and then transpose one column into a row.
I tried following a tutorial for reading a csv and then one for writing but the data doesnt stay saved to the list when I try to write the row.
import csv
f = open('bond-dist-rep.csv')
csv_f = csv.reader(f)
bondlength = []
with open("bond-dist-rep.csv") as f:
for row in csv_f:
bondlength.append(row[1])
print (bondlength)
print (len(bondlength))
with open('joined.csv', 'w', newline='') as csvfile:
csv_a = csv.writer (csvfile, delimiter=',',quotechar='"',
quoting=csv.QUOTE_ALL)
csv_a.writerow(['bondlength'])
with open('joined.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
print(row)
print(row[0])
f.close()
The matter is that you only read the first value of each line and write only a string in the new file.
In order to transpose the read lines, you can use the zip function.
I also delete the first open function which is useless because of the good use of with for opening the file.
Here the final code:
import csv
bondlength = []
with open("bond-dist-rep.csv") as csv_f:
read_csv = csv.reader(csv_f)
for row in read_csv:
bondlength.append(row)
# delete the header if you have one
bondlength.pop(0)
with open('joined.csv', 'w') as csvfile:
csv_a = csv.writer (csvfile, delimiter=',')
for transpose_row in zip(*bondlength):
csv_a.writerow(transpose_row)
Not to over write csv, I tried 'a' but I get no separators.
Tried to replaced 'w' with 'a', but get no separator just mismash
import csv
with open('csv/1.n04', 'r') as csvFile:
reader = csv.reader(csvFile)
s = "".join(row[0] for row in reader)
a = s.count('0')
b = s.count('1')
csvFile.close()
import csv
f = open('csv/sum1.n04', 'w')
if b>=1:
f.write(str((a+b)/b))
else:
f.write(str(a))
f.close
import os
os.remove("csv/1.n04")
print ('Klar')
No error messages, it just over write csv file
I'm trying to print certain values into a CSV from a list I'm passing through. Here is my function:
current = datetime.datetime.now() #defining datetime for use in function
def write_csv(custody_parts):
with open((current.strftime("%m_%d_%y_%H_%M_%S")) + '.csv', 'w', newline='') as csvfile:
csvfile = io.StringIO()
fieldnames = ['serial', 'user', 'time']
writer = csv.DictWriter(csvfile, extrasaction='ignore', fieldnames=fieldnames)
writer.writeheader()
writer.writerows(custody_parts)
csvfile.getvalue()
print(csvfile.getvalue())
return(csvfile.getvalue())```
Then I call it with the list I'm trying to pass through:
write_csv(parts)
and it creates the file: 06_06_18_12_13_53.csv
and prints to the screen:
serial,user,time
serial1,user1,date1
serial2,user2,date2
serial3,user3,date3
but the file it creates is empty, so it isn't writing to the file it is creating.
Can someone point me in the right direction?
Thanks ~
EDIT:
I ended up going with this instead:
def write_csv(custody_parts):
current = datetime.datetime.now()
with open((current.strftime("%m_%d_%y_%H_%M_%S")) + '.csv', 'w', newline='') as csvfile:
custodywriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
custodywriter.writerow(['serial', 'user', 'time'])
for i in custody_parts:
x = [i["serial"],i["user"],i["time"]]
custodywriter.writerow(x)
custodywriter.writerow(["End of Report"])
Been having trouble with using variables in a new dirpath when using with open to create a csv file.
I can create the dirpath fine along with the .csv filename and add them with os.path.join .
But when i try to use the entire correctly formatted path in the open statement it is adding an extra backslash next to the current backslashes in the string?? could not find any documentation on why this may be happening.
This is the error
FileNotFoundError: [Errno 2] No such file or directory: 'Logs\\14-05-2018\\dfg.csv'
If i simply write the following i can create the subfolders and csvs no problemo.
with open(Logs\1234\asd.csv) 'w') as csvfile:
So this is what i am getting an error in on line 43 which is the with open(csvlogPath, 'w') as csvfile: I have tried with r instead of 'w' and same problem. At a loss after two days time to ask you guys. :)
import csv, os.path
#Get and create csv filename string
logFN = input("Please name your trade.\n#")
suffix = '.csv'
csvname = logFN + suffix
#Create dir name string
t = time.strftime("%d-%m-%Y-%H%M")
dirsname = os.path.join('Logs', t)
csvlogPath = os.path.join(dirsname,csvname)
with open(csvlogPath, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['get', 'new', 'values', 'later'])
with open(csvlogPath, 'w') as f:
reader = csv.reader(f)
# read file row by row
rwsCount = 0
for row in reader:
print (row, rwsCount)
rwsCount += 1
line_number = rwsCount-1
print (line_number)
with open(csvlogPath, 'w') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
text = mycsv[line_number][0]
print (text)
you need to make the dirs in python you can use os.makedirs(path) like so:
csvlogPath = os.path.join(dirsname,csvname)
os.makedirs(os.path.dirname(csvlogPath), exist_ok=True)
with open(csvlogPath, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['get', 'new', 'values', 'later'])
With open, use 'r' to read and 'w' to write so you need to change the next to opens to:
with open(csvlogPath, 'r') as f:
reader = csv.reader(f)
# read file row by row
rwsCount = 0
for row in reader:
print (row, rwsCount)
rwsCount += 1
line_number = rwsCount-1
print (line_number)
with open(csvlogPath, 'r') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
text = mycsv[line_number][0]
test this code :
import csv, os.path
#Get and create csv filename string
import time
logFN = input("Please name your trade.\n#")
suffix = '.csv'
csvname = logFN + suffix
#Create dir name string
t = time.strftime("%d-%m-%Y-%H%M")
dirsname = os.path.join('Logs', t)
###########################
if not os.path.exists(dirsname):
os.makedirs(dirsname)
###########################
csvlogPath = os.path.join(dirsname,csvname)
with open(csvlogPath, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', lineterminator='\n', quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['get', 'new', 'values', 'later'])
with open(csvlogPath, 'r') as f:
reader = csv.reader(f)
# read file row by row
rwsCount = 0
for row in reader:
print (row, rwsCount)
rwsCount += 1
line_number = rwsCount-1
print (line_number)
with open(csvlogPath, 'r') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
text = mycsv[line_number][0]
print (text)