how to make your new user inputs get updated into csv file - python-3.x

user_input = ['email','fname','lname','password','gender','cohort','program','ID']
with open('users.csv','w') as inFile:
writer = csv.DictWriter(inFile, fieldnames=user_input)
writer.writerow({'email':email,'fname': fname ,'lname':lname , 'password':password, 'gender':gender, 'cohort':cohort, 'program':program,'ID':ID})
the issue with this is that, everytime a new user input is entered on the webpage, the former one is discarded

To append more data to a file to be opened, you should use 'a' mode for opening. In 'w' mode, the file is truncated first. See the open function documentation for more information.
# ...
user_input = ['email','fname','lname','password','gender','cohort','program','ID']
with open('users.csv', 'a') as f:
writer = csv.DictWriter(f, fieldnames=user_input)
writer.writerow({'email':email, 'fname':fname, 'lname':lname, 'password':password, 'gender':gender, 'cohort':cohort, 'program':program, 'ID':ID})

Related

CSV appears empty just after I write sth onto it

while True:
with open('secrets.csv', 'a', encoding='utf-8', newline='') as sf:
secrets = ts_functions.get_secrets(driver)
new_secrets = []
writer_1 = csv.DictWriter(sf, keys)
not_first_run = exists('last_secret.csv')
# THIS IF STATEMENT RUNS FROM THE 2ND ITERATION ONWARDS, NOT IN THE 1ST ONE
if not_first_run:
with open('last_secret.csv', 'r', encoding='utf-8', newline='') as rlf:
reader = csv.DictReader(rlf)
for row in reader:
# THERE SHOULD ONLY BE 1 ROW BUT THE READER APPEARS EMPTY
only_secret = row
for secret in secrets:
if not_first_run:
if only_secret == secret:
break
new_secrets.append(secret)
new_secrets.reverse()
for secret in new_secrets:
writer_1.writerow(secret)
with open('last_secret.csv', 'w', encoding='utf-8', newline='') as wlf:
writer_2 = csv.DictWriter(wlf, keys)
# HERE I WRITE ONE ROW(JUST ONE) IN THE 'last_secret.csv' FILE BUT WHEN THE SECOND
# ITERATION CHECKS THE IF STATEMENT THERE IS AN ERROR BECAUSE THE READER IS EMPTY
writer_2.writerow(new_secrets[-1])
sleep(10)
My program is about web scraping but I got that part covered(I can post the related functions too if needed) but I think the problem most likely is just how I am writing the 'last_secret.csv' file. When I open the file myself in Visual Studio there is the line Ii am expecting but when the reader object reads it It appears as empty and that causes an error because I didn't declare the only_secret variable(it's None).
As I said before I was expecting the reader object to read 1 row(the only one I write onto the file) but instead It reads nothingg as if the file where empty, when I check the file for myself the line is there but the reader says otherwise. I tried changing the write mode to append but It changed nothing.
Thx in advance

CSV file appears empty when it's not

def main():
driver = ts_functions.start_selenium()
# A good practice to access the elements in the csv is using a dict
keys = ['age', 'numeration', 'popularity', 'country', 'num_of_comments', 'time', 'sex', 'text']
while True:
# For a file to change you gotta close it first maybe?
with open('secrets.csv', 'a', encoding='utf-8', newline='') as sf:
secrets = ts_functions.get_secrets(driver)
new_secrets = []
writer = csv.DictWriter(sf, keys)
not_first_run = exists('last_secret.csv')
if not_first_run:
with open('last_secret.csv', 'r', encoding='utf-8', newline='') as rlf:
reader = csv.DictReader(rlf)
for row in reader:
only_secret = row #There should be 1 element but it's empty
for secret in secrets:
if not_first_run:
if only_secret == secret: #<---- Error IS HERE
break
new_secrets.append(secret)
new_secrets.reverse()
for secret in new_secrets:
writer.writerow(secret)
# I use 'w' here because I only want the last message of the last while loop
# so it's ok if it erases the previous content
with open('last_secret.csv', 'w', encoding='utf-8', newline='') as wlf:
writer = csv.DictWriter(wlf, keys)
# I write the only line in the file here but then it appears empty even though
# I can see the file being created and line being written
writer.writerow(new_secrets[-1])
sleep(10)
This is my main function, the two helper functions do their job properly, but if neccessary I can share them, my program tries to scrape data from a website that uploads messages live(like twitch comments) and then store the messages(secrets) in one file(secrets.csv), then I want to use the other csv file(last_secret.csv) to identify wheter a message is new or has already been scraped(I store the newest message there and compare them to the next batch of messages, when it finds one that is the same, it stops).
The problem is that the in the second run when I try to read from the latter file the reader object is empty and an error occurs.
I checked the helper functions and they are fine, the problem is with the CSVs I changed the 'last_secret.csv' mode from 'w' to 'a' and it didn't solve anything. I checked the file's contents in the same run(the 1st one) just after writing the line in it and it also appeared empty.
This is my firt question srry for the bad formatting :p

Editing data in a CSV file using Python

I am writing data to a notepad (.txt) file in Python. Entities are separated with commas (','). The file is called 'Database2.txt'.
The variable 'yearlynew' contains a float value that will replace a piece of existing data. I have tried various methods of writing said data to the file but cannot get it to work.
This is the code that I have so far:
g = open('Database2.txt', 'r')
for line in g:
CurrentLine = line.split(',')
if CurrentLine[0] == account:
g = open('Database2.txt', 'a')
#CurrentLine[3] = yearlynew
#g(CurrentLine[3]).write(yearlynew)
#CurrentLine[3].write(yearlynew)
Anything with a '#' before it indicates a failed method that I have tried. If anybody has any suggestions on how to do this I would be very grateful.
I should note that CurrentLine[0] identifies usernames (which are in the first column of the file) so that whichever user is logged in can edit their data and their data only. 'account' holds the username of the user that is currently logged in, and allows the program to trace through the file to find the user's statistics. User data such as names and passwords are stored in a separate file and are not needed for this function.
This is the contents of the file:
Ellie121,0.16,5,60,
Sam232,0.2,6,72,
Heather343,0.1,3,36,
Connor454,0.35,10.5,126,
Ryan565,0.15,4.5,54,
Matthew676,0.22,6.6,79.2,
I don’t think that you can just insert text into a file. I think you will have to write the new data to a different file and then rename it. This is how I would go about it :
import os
account = 'Sam232'
yearlynew = '3.14159'
temp = open ('temporary_file', 'w')
g = open ('Database2.txt', 'r')
for line in g :
CurrentLine = line.split (',')
if CurrentLine [0] == account :
CurrentLine [1] = yearlynew
OutputLine = ','.join (CurrentLine)
temp.write (OutputLine)
g.close ()
temp.close ()
os.remove ('Database2.txt')
os.rename ('temporary_file', 'Database2.txt')

How to edit a line in a notepad file using python

I am trying to edit a specific line of a notepad file using Python 3. I can read from any part of the file and write to the end of it, however whenever I have tried editing a specific line, I am given the error message 'TypeError: 'int' object is not subscriptable'. Does anybody know how I could fix this?
#(This was my first attempt)
f = open('NotepadTester.txt', 'w')
Edit = input('Enter corrected data')
Line = int(input('Which line do you want to edit?'))
f.write(Edit)[Line-1]
f.close()
main()
#(This was my second attempt)
f = open('NotepadTester.txt', 'w')
Line = int(input('Which line do you want to edit?'))
Edit = input('Enter corrected data')
f[Line-1] = (Edit)
main()
you can't directly 'edit' a line in a text file as far as I know. what you could do is read the source file src to a variable data line-by-line, edit the respective line and write the edited variable to another file (or overwrite the input file) dst.
EX:
# load information
with open(src, 'r') as fobj:
data = fobj.readlines() # list with one element for each text file line
# replace line with some new info at index ix
data[ix] = 'some new info\n'
# write updated information
with open(dst, 'w') as fobj:
fobj.writelines(data)
...or nice and short (thanks to Aivar Paalberg for the suggestion), overwriting the input file (using open with r+):
with open(src, 'r+') as fobj:
data = fobj.readlines()
data[ix] = 'some new info\n'
fobj.seek(0) # reset file pointer...
fobj.writelines(data)
You should probably load all the lines into memory first, modify it from there, and then write the whole thing to a file.
f = open('NotepadTester.txt', 'r')
lines = f.readlines()
f.close()
Which_Line = int(input('Which line do you want to edit? '))
Edit = input('Enter corrected data: ')
f = open("NotepadTester.txt",'w')
for i,line in enumerate(lines):
if i == Which_Line:
f.writelines(str(Edit)+"\n")
else:
f.writelines(line)
f.close()

add new row to numpy using realtime reading

I am using a microstacknode accelerometer and intend to save it into csv file.
while True:
numpy.loadtxt('foo.csv', delimiter=",")
raw = accelerometer.get_xyz(raw=True)
g = accelerometer.get_xyz()
ms = accelerometer.get_xyz_ms2()
a = numpy.asarray([[raw['x'],raw['y'],raw['z']]])
numpy.savetxt("foo.csv",a,delimiter=",",newline="\n")
However, the saving is only done on 1 line. Any help given? Still quite a noobie on python.
NumPy is not the best solution for this type of things.
This should do what you intend:
while True:
raw = accelerometer.get_xyz(raw=True)
fobj = open('foo.csv', 'a')
fobj.write('{},{},{}\n'.format(raw['x'], raw['y'], raw['z']))
fobj.close()
Here fobj = open('foo.csv', 'a') opens the file in append mode. So if the file already exists, the next writing will go to the end of file, keeping the data in the file.
Let's have look at your code. This line:
numpy.loadtxt('foo.csv', delimiter=",")
reads the whole file but doe not do anything with the at it read, because you don't assign to a variable. You would need to do something like this:
data = numpy.loadtxt('foo.csv', delimiter=",")
This line:
numpy.savetxt("foo.csv",a,delimiter=",",newline="\n")
Creates a new file with the name foo.csv overwriting the existing one. Therefore, you see only one line, the last one written.
This should do the same but dos not open and close the file all the time:
with open('foo.csv', 'a') as fobj:
while True:
raw = accelerometer.get_xyz(raw=True)
fobj.write('{},{},{}\n'.format(raw['x'], raw['y'], raw['z']))
The with open() opens the file with the promise to close it even in case of an exception. For example, if you break out of the while True loop with Ctrl-C.

Resources