I created a function and the idea is that if the "main" file (transactions_ledger.csv) doesn't exist I need to create it and append the new file (user input- which file) to it.
The code is working BUT the new file has an additional row in between with no contents.
note: I can not use pandas
In advance thank you for helping me.
Here is my code and the output:
from csv import writer
import os
from operator import itemgetter
# Create the ImportFunction
def ImportFunction():
# First step is to ask the user what file they would like to import
which_file = input("Which file would you like to import?")
#Second step is to load the file into python. For this I will use the With open statement in a try except block so the program doesnt crash if the file doesnt exist
try:
# i need to preform all actions!
with open(which_file,'r') as file:
user_file = file.read()
print(user_file)
except:
print("Sorry the user file can't be found. Please try again.")
#Third step is to open the transaction_ledger.csv. If the file doesnt exist create it if it does then preform actions
try:
#Have to do this using append method
with open('transaction_ledger.csv','r') as file:
file_content_transaction_ledger = file.read()
print(file_content_transaction_ledger)
except:
#Open user file and read it to be able to append to the new empty file transaction_ledger.csv
with open(which_file,'r') as old_file:
reader_obj = csv.reader(old_file) #read the current csv file
with open('transaction_ledger.csv', 'w') as new_file:
writer_obj = csv.writer(new_file, delimiter=",")
for data in reader_obj:
#loop through the read data and write each row in transaction_Ledger.csv
writer_obj.writerow(data)
print("New file created and filled in with old file data")
ImportFunction()
with open('transaction_ledger.csv','r') as file:
file_content_transaction_ledger = file.read()
print(file_content_transaction_ledger) ```
**Here is the output:**
Which file would you like to import? transactions_q1.csv
9547632,Arasaka,3/1/2022,6500,PENDING
1584037,Militech,3/15/2022,3000,COMPLETE
9433817,Arasaka,4/1/2022,450,COMPLETE
9462158,Arasaka,4/29/2022,900,PENDING
New file created and filled in with old file data
9547632,Arasaka,3/1/2022,6500,PENDING
1584037,Militech,3/15/2022,3000,COMPLETE
9433817,Arasaka,4/1/2022,450,COMPLETE
9462158,Arasaka,4/29/2022,900,PENDING
I found a solution. I need to specify inside the writer_obj = csv.writer(new_file,delimiter=",") the following:
lineterminator = "\n"
Related
I have a GUI.exe (converted from a .py file) that has entry boxes.
I want to get the values that was entered in entry boxes from this GUI.exe and use it in a Python file (.py).
Is that possible? Can anyone help me in this?
You could create a csv by doing
import csv
with open('GUI_data.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(all_data) #all_data should be list of all data
With the other file you could do this to load data out as a list
with open('GUI_data.csv', newline='') as file:
file_data = csv.reader(file, delimiter=',')
transfered_data = next(file_data) # transfered_data now holds the info as a list
i'm new to python and I've got a large json file that I need to convert to csv - below is a sample
{ "status": "success","Name": "Theresa May","Location": "87654321","AccountCategory": "Business","AccountType": "Current","TicketNo": "12345-12","AvailableBal": "12775.0400","BookBa": "123475.0400","TotalCredit": "1234567","TotalDebit": "0","Usage": "5","Period": "May 11 2014 to Jul 11 2014","Currency": "GBP","Applicants": "Angel","Signatories": [{"Name": "Not Available","BVB":"Not Available"}],"Details": [{"PTransactionDate":"24-Jul-14","PValueDate":"24-Jul-13","PNarration":"Cash Deposit","PCredit":"0.0000","PDebit":"40003.0000","PBalance":"40003.0000"},{"PTransactionDate":"24-Jul-14","PValueDate":"23-Jul-14","PTest":"Cash Deposit","PCredit":"0.0000","PDebit":"40003.0000","PBalance":"40003.0000"},{"PTransactionDate":"25-Jul-14","PValueDate":"22-Jul-14","PTest":"Cash Deposit","PCredit":"0.0000","PDebit":"40003.0000","PBalance":"40003.0000"},{"PTransactionDate":"25-Jul-14","PValueDate":"21-Jul-14","PTest":"Cash Deposit","PCredit":"0.0000","PDebit":"40003.0000","PBalance":"40003.0000"},{"PTransactionDate":"25-Jul-14","PValueDate":"20-Jul-14","PTest":"Cash Deposit","PCredit":"0.0000","PDebit":"40003.0000","PBalance":"40003.0000"}]}
I need this to show up as
name, status, location, accountcategory, accounttype, availablebal, totalcredit, totaldebit, etc as columns,
with the pcredit, pdebit, pbalance, ptransactiondate, pvaluedate and 'ptest' having new values each row as the JSON file shows
I've managed to put this script below together looking online, but it's showing me an empty csv file at the end. What have I done wrong? I have used the online json to csv converters and it works, however as these are sensitive files I'm hoping to write/manage with my own script so I can see exactly how it works. Please see below for my python script - can I have some advise on what to change? thanks
import csv
import json
infile = open("BankStatementJSON1.json","r")
outfile = open("testing.csv","w")
writer = csv.writer(outfile)
for row in json.loads(infile.read()):
writer.writerow(row)
import csv, json, sys
# if you are not using utf-8 files, remove the next line
sys.setdefaultencoding("UTF-8") # set the encode to utf8
# check if you pass the input file and output file
if sys.argv[1] is not None and sys.argv[2] is not None:
fileInput = sys.argv[1]
fileOutput = sys.argv[2]
inputFile = open("BankStatementJSON1.json","r") # open json file
outputFile = open("testing2.csv","w") # load csv file
data = json.load("BankStatementJSON1.json") # load json content
inputFile.close() # close the input file
output = csv.writer("testing.csv") # create a csv.write
output.writerow(data[0].keys()) # header row
for row in data:
output.writerow(row.values()) # values row
This works for the JSON example you posted. The issue is that you have nested dict and you can't create sub-headers and sub rows for pcredit, pdebit, pbalance, ptransactiondate, pvaluedate and ptest as you want.
You can use csv.DictWriter:
import csv
import json
with open("BankStatementJSON1.json", "r") as inputFile: # open json file
data = json.loads(inputFile.read()) # load json content
with open("testing.csv", "w") as outputFile: # open csv file
output = csv.DictWriter(outputFile, data.keys()) # create a writer
output.writeheader()
output.writerow(data)
Make sure you're closing the output file at the end as well.
elif addordelete = "delete":
whichdelete = input("What thing do you want to delete? ")
GameCharacter.csv
I want to know how to delete a specific cell in a csv file through a python input.
For example if the user in the python program says that they want to delete MP40 from the file, then it should delete. Can someone explain how to do this in simple terms as possible (I'm kind of a python noob. Code is appreciated.
You'll have to import the whole CSV into python, process it and saving it back to file.
Here's a simple snippet which opens the CSV, processes it asking what you want to delete, then saves it back again.
You can start from this code to get what you need.
import csv
try:
csvfile = open('testcsv.csv','rb')
table = [row for row in csv.reader(csvfile)] #further parameters such as delimiters on doc
whichdelete = whichdelete = input("What thing do you want to delete? ")
header = table.pop(0) #remove header and save it to header variable
res = [row for row in table if whichdelete not in row] #save result without the row you want to delete
csvfile.close()
except IOError:
print "File not found"
quit()
try:
resultFile = open('resultCSV.csv','wb+')
writer = csv.writer(resultFile)
writer.writerow(header)
for row in res:
writer.writerow(row)
resultFile.close()
except IOError:
print "Error creating result file"
quit()
I am only at the beginning of my python programming way.
f = csv.reader(open('andr.csv'), delimiter=',')
andr=[]
for row in f:
This is for specific .csv file in my comp. I want to INPUT random file
file=print(input('File to analyze: '))
f = csv.reader(open(file), delimiter=',')
andr=[]
for row in f:
This obviously doesn't work. As I've already told, I am just starting to study python and it is probably very easy, but I am stuck here. I appreciate any help.
This is how basic prompts and csv reader work
import csv
while True:
filename = input('File to analyze: ')
try:
with open(filename, endline='') as fp:
for row in csv.reader(fp):
print(row)
break
except Exception as e:
print("Invalid file {} ({}), try again.".format(filename, e))
You can use glob module to get any filenames with specific extension(.csv) from any directory.
Then by using Random module you can randomly select any one file,
Please try this code,
import random
import glob
#total_files contains all CSV file names.
total_files = glob.glob(('*.csv'))
#randomly selecting one csv file.
file = random.choice(filee)
f = csv.reader(open(file), delimiter=',')
andr=[]
for row in f:
Please let me know in terms of any queries.
Is there a method of creating a text file without opening a text file in "w" or "a" mode? For instance If I wanted to open a file in "r" mode but the file does not exist then when I catch IOError I want a new file to be created
e.g.:
while flag == True:
try:
# opening src in a+ mode will allow me to read and append to file
with open("Class {0} data.txt".format(classNo),"r") as src:
# list containing all data from file, one line is one item in list
data = src.readlines()
for ind,line in enumerate(data):
if surname.lower() and firstName.lower() in line.lower():
# overwrite the relevant item in data with the updated score
data[ind] = "{0} {1}\n".format(line.rstrip(),score)
rewrite = True
else:
with open("Class {0} data.txt".format(classNo),"a") as src:
src.write("{0},{1} : {2}{3} ".format(surname, firstName, score,"\n"))
if rewrite == True:
# reopen src in write mode and overwrite all the records with the items in data
with open("Class {} data.txt".format(classNo),"w") as src:
src.writelines(data)
flag = False
except IOError:
print("New data file created")
# Here I want a new file to be created and assigned to the variable src so when the
# while loop iterates for the second time the file should successfully open
At the beginning just check if the file exists and create it if it doesn't:
filename = "Class {0} data.txt"
if not os.path.isfile(filename):
open(filename, 'w').close()
From this point on you can assume the file exists, this will greatly simplify your code.
No operating system will allow you to create a file without actually writing to it. You can encapsulate this in a library so that the creation is not visible, but it is impossible to avoid writing to the file system if you really want to modify the file system.
Here is a quick and dirty open replacement which does what you propose.
def open_for_reading_create_if_missing(filename):
try:
handle = open(filename, 'r')
except IOError:
with open(filename, 'w') as f:
pass
handle = open(filename, 'r')
return handle
Better would be to create the file if it doesn't exist, e.g. Something like:
import sys, os
def ensure_file_exists(file_name):
""" Make sure that I file with the given name exists """
(the_dir, fname) = os.path.split(file_name)
if not os.path.exists(the_dir):
sys.mkdirs(the_dir) # This may give an exception if the directory cannot be made.
if not os.path.exists(file_name):
open(file_name, 'w').close()
You could even have a safe_open function that did something similar prior to opening for read and returning the file handle.
The sample code provided in the question is not very clear, specially because it invokes multiple variables that are not defined anywhere. But based on it here is my suggestion. You can create a function similar to touch + file open, but which will be platform agnostic.
def touch_open( filename):
try:
connect = open( filename, "r")
except IOError:
connect = open( filename, "a")
connect.close()
connect = open( filename, "r")
return connect
This function will open the file for you if it exists. If the file doesn't exist it will create a blank file with the same name and the open it. An additional bonus functionality with respect to import os; os.system('touch test.txt') is that it does not create a child process in the shell making it faster.
Since it doesn't use the with open(filename) as src syntax you should either remember to close the connection at the end with connection = touch_open( filename); connection.close() or preferably you could open it in a for loop. Example:
file2open = "test.txt"
for i, row in enumerate( touch_open( file2open)):
print i, row, # print the line number and content
This option should be preferred to data = src.readlines() followed by enumerate( data), found in your code, because it avoids looping twice through the file.