I am trying to make a .csv splitter.
I should take x number og lines and make new csv files with that number of lines in them from the original csv file.
import os
import csv
fileDir = ('C:\\somedir\\')
fName = input('Write the file name here, without extention: ')
lineNo = int(input('Number of lines pr csv file: '))
fNameF = (fName + '.csv')
filehandler = (fileDir + fNameF)
def split(filehandler, delimiter=',', row_limit=lineNo,
output_name_template='fName_%s.csv', output_path=fileDir, keep_headers=True):
reader = csv.reader(filehandler, delimiter=delimiter)
current_piece = 1
current_out_path = os.path.join(
output_name_template % current_piece
)
current_out_writer = csv.writer(open(current_out_path, 'w'))
current_limit = row_limit
if keep_headers:
headers = reader.next()
current_out_writer.writerow(headers)
for i, row in enumerate(reader):
if i + 1 > current_limit:
current_piece += 1
current_limit = row_limit * current_piece
current_out_path = os.path.join(
output_name_template % current_piece
)
current_out_writer = csv.writer(open(current_out_path, 'w'))
if keep_headers:
current_out_writer.writerow(headers)
current_out_writer.writerow(row)
It runs the script, but nothing happens, could someone help me?
I think you have not called the function split() at all. And you have also passed all the values in function as parameters. Check if you get output with the following code.
import os
import csv
fileDir = ('C:\\somedir\\')
fName = input('Write the file name here, without extention: ')
lineNo = int(input('Number of lines pr csv file: '))
fNameF = (fName + '.csv')
filehandler = (fileDir + fNameF)
def split(filehandler, delimiter, row_limit,
output_name_template, output_path, keep_headers):
reader = csv.reader(filehandler, delimiter=delimiter)
current_piece = 1
current_out_path = os.path.join(
output_name_template % current_piece
)
current_out_writer = csv.writer(open(current_out_path, 'w'))
current_limit = row_limit
if keep_headers:
headers = reader.next()
current_out_writer.writerow(headers)
for i, row in enumerate(reader):
if i + 1 > current_limit:
current_piece += 1
current_limit = row_limit * current_piece
current_out_path = os.path.join(
output_name_template % current_piece
)
current_out_writer = csv.writer(open(current_out_path, 'w'))
if keep_headers:
current_out_writer.writerow(headers)
current_out_writer.writerow(row)
split(filehandler,',', lineNo,'fName_%s.csv', fileDir, True)
Related
I have this code where I'm merging the content of a lot of .txt files into only one .txt file.
Those .txt files are generated according to a date (in this case 01-12-2022, but in the folder there are a lot of logs of other dates like 02-12-2022, 03-12-2022, it logs for 9 days), how do I make the merge with all the dates? And also make this dynamic, like when I get those logs in February for example, this works the same (merge all the logs from all the nine days into only one .txt file)
The code:
zip_file = r'C:\Users\Viniz\Downloads\devkmbe-5511001_05-12-2022_00_20_09.zip'
pastaLogs = 'devkmbe-5511001_05-12-2022_00_20_09'
audioCapture = pastaLogs + '/AudioCaptureMemoryUsage_01_12_2022.txt'
audioMatching = pastaLogs + '/AudioMatchingMemoryUsage_01_12_2022.txt'
audioWatermarking = pastaLogs + '/AudioWatermarkingMemoryUsage_01_12_2022.txt'
awmDetection = pastaLogs + '/AwmDetectionLog_01_12_2022.txt'
backendMemory = pastaLogs + '/BackendMemoryUsage_01_12_2022.txt'
batteryCharge = pastaLogs + '/BatteryChargeControlLog_01_12_2022.txt' #f6
batteryLife = pastaLogs + '/BatteryLifeMonitorLog_01_12_2022.txt'
bluetoothComms = pastaLogs + '/BluetoothCommsLog_01_12_2022.txt'
btrcLog = pastaLogs + '/BTRCLog_01_12_2022.txt'
dashboardCommunication = pastaLogs + '/DashBoard_Communication_01_12_2022.txt'
deliveryService = pastaLogs + '/DeliveryServiceMemoryUsage_01_12_2022.txt'
dmbeLog = pastaLogs + '/DMBELog_01_12_2022.txt'
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
with zip_ref.open(audioCapture) as f1, zip_ref.open(audioMatching) as f2,zip_ref.open(audioWatermarking) as f3, zip_ref.open(awmDetection) as f4,zip_ref.open(backendMemory) as f5, zip_ref.open(batteryCharge) as f6,zip_ref.open(batteryLife) as f7,zip_ref.open(bluetoothComms) as f8,zip_ref.open(btrcLog) as f9, zip_ref.open(dashboardCommunication) as f10, zip_ref.open(deliveryService) as f11 :
primeiroLog = f1.read()
segundoLog = f2.read()
terceiroLog = f3.read()
quartoLog = f4.read()
quintoLog= f5.read()
sextoLog = f6.read()
setimoLog = f7.read()
oitavoLog = f8.read()
nonoLog = f9.read()
decimoLog = f10.read()
decimoprimeiroLog= f11.read()
# Concatenar os logs
merged_content = primeiroLog + segundoLog + terceiroLog + quartoLog + quintoLog + sextoLog + setimoLog + oitavoLog + nonoLog + decimoLog + decimoprimeiroLog
# Mergear os logs em um novo
new_file = input("Coloque o nome do novo arquivo: ")
with open(new_file, 'wb') as new_f:
new_f.write(merged_content)
print("VocĂȘ mergeou com sucesso!")
print("Deseja ver o novo arquivo ? (s / n): ")
check = input()
if check == 'n':
exit()
else:
print()
c = open(new_file, "r")
print(c.read())
c.close()
Trying to make this work, but with some troubles :(
My request are downloading the images, but not properly. The images are empty. I know I am close to getting it correct, but I am not sure what I am missing--any help would be greatly appreciated!!!
fname ='testData.csv'
s = "dos2unix {}".format(fname)
inner_quote_grabber = re.compile(r'("[^"]*")', flags=re.M)
parenth_grabber = re.compile(r'\(([^)]*)\)', flags=re.M)
new_rows = []
matter = [0, 3,4]
file_counter = 0
file_prefix = 'images/'
file_out = 'outfile.csv'
with open(fname, 'r') as c:
reader = csv.reader(c, delimiter=',')
for row in reader:
t_row = []
#print( len(row), row)
for i in matter:
t_row.append(row[i])
last_row = []
print(row[7])
for group in parenth_grabber.findall(row[7]):
print('grabbing ', group)
file_counter += 1
click_clack = file_prefix + 'file_' + str(file_counter) +'.jpg'
print('group:', group)
req = requests.get(group)
print('status_code', req.status_code)
last_row.append(click_clack)
with open(click_clack, 'wb') as f:
req.raw.decode_content = True
shutil.copyfileobj(req.raw, f)
#f.write(text)
file_counter += 1
t_row.append(', '.join(last_row))
with open(file_out, 'a') as f:
writer = csv.writer(f)
writer.writerow(t_row)
I am trying to split an hash and writing it to different csv files but it throws error
def split(self,filehandler, delimiter=',', row_limit=1000,
output_name_template='output_%s.csv', output_path='.', keep_headers=True):
#reader = csv.reader(filehandler, delimiter=delimiter)
current_piece = 1
current_out_path = os.path.join(
output_path,
output_name_template % current_piece
)
current_out_writer = csv.writer(open(current_out_path, 'w'), delimiter=',')
current_limit = row_limit
if keep_headers:
headers = filehandler.next()
current_out_writer.writerow(headers)
for i, row in enumerate(filehandler):
if i + 1 > current_limit:
current_piece += 1
current_limit = row_limit * current_piece
current_out_path = os.path.join(
output_path,
output_name_template % current_piece
)
current_out_writer = csv.writer(open(current_out_path, 'w'), delimiter=',')
if keep_headers:
current_out_writer.writerow(self.headers)
current_out_writer.writerow(row)
split(self.merged_data)
first error
headers = filehandler.next()
AttributeError: 'dict' object has no attribute 'next'
Second error
for i, row in enumerate(filehandler):
TypeError: 'Merge_project' object is not iterable
how to fix the error and split the hash and write out to different csv file?
I have 2 files:
res_my_inp.csv : http://ge.tt/8Fa4d512/v/0?c
res_my_out.csv : http://ge.tt/8Fa4d512/v/1?c
I want to get matrix from res_my_inp.csv and array from res_my_out.csv (get only 1 column)
My code:
import csv
in_filepath = os.path.abspath( "res_my_inp.csv" )
out_filepath = os.path.abspath( "res_my_out.csv" )
def ReadCsv( fileName, mode ):
in_file = open( fileName )
reader = csv.reader( in_file, delimiter = '\t', quotechar = '"' )
if ( mode == 'input' ):
data = [[]]
for row in reader:
data.append( row )
elif( mode == 'output' ):
column = 1
data = []
for row in reader:
data.append( int( row[column] ) )
data.pop(0)
return data
X = np.asarray( ReadCsv( in_filepath, 'input' ), 'int' )
Y = np.asarray( ReadCsv( out_filepath, 'output' ), 'int' )
print(len(X))
print(len(Y))
Why len(X) = 100, but len(Y) = 99 ?
The length discrepancy is because this line:
data = [[]]
is different from this line:
data = []
In the 'input' branch you've got an empty list at the start of your data list that isn't present in the 'output' branch.
I'm trying to compare CSV files, I have 2 CSV Files - CSVFileOne, CSVFileTwo.
my desired output is printing CSVFileOne but only the rows that not existing in CSVFileTwo.
My code:
input_file = CSVFileOne
ABGSOne = []
with open(input_file, encoding='UTF-8') as fone:
rowsOne = csv.reader(fone,delimiter=",",lineterminator="\n")
next(rowsOne, None)
for rowOne in rowsOne:
abbgone = {}
abbgone['astringOne'] = row[0]
abbgone['bstringOne'] = row[1]
abbgone['cstringOne'] = row[2]
abbgone['dstringOne'] = row[3]
ABGSOne.append(abbgone)
input_fileTwo = CSVFileTwo
ABGSTwo = []
with open(input_fileTwo, encoding='UTF-8') as ftwo:
rowsTwo = csv.reader(ftwo,delimiter=",",lineterminator="\n")
next(rowsTwo, None)
for rowTwo in rowsTwo:
abbgtwo = {}
abbgtwo['astringTwo'] = row[0]
abbgtwo['bstringTwo'] = row[1]
abbgtwo['cstringTwo'] = row[2]
abbgtwo['dstringTwo'] = row[3]
ABGSOne.append(abbgTwo)
for abbgone in ABGSOne:
if abbgone['bstringOne'] == abbgtwo['bstringTwo']:
print('abbgone['bstringOne']
try this out .
with open('CSVFileOne.csv', 'r') as t1, open('CSVFileTwo.csv', 'r') as t2:
fileone = t1.readlines()
filetwo = t2.readlines()
with open('Desired.csv', 'w') as outFile:
for line in filetwo:
if line not in fileone:
outFile.write(line)