I have this code which I want to add data from the user input to each column and rows:
import csv
import datetime
import calendar
days = [i for i in range(1,32,1)]
days = (','.join(map(str,days))).strip() # To remove square brackets
field = ['fingerprint', 'month', 'year',days]
for i in range(4):
finger1 = input("input the fingerprints: ")
finger = ''.join(finger1)
year = input("Enter Year")
month = input("enter month")
def main():
with open('shoft.csv', 'w', newline='') as file:
add = csv.writer(file, quoting=csv.QUOTE_NONE, escapechar='/')
add.writerow(field)
add.writerows(finger)
def addRow():
pass
def addColumn():
pass
if __name__ == "__main__":
main()
As I want to ask for times for 4 users to add their fingerprint number then it adds them to fingerprint column in four rows, then add year which same for all four rows with month And for days it automatically adds x for one and 6 for next day except Fridays should be always x. days numbers it should be just number but couldn't find solution to remove quotes other than adding escapechar with slash.
like this :
code,month,year,1,2,3,4,5,6,..31
c633,5 ,2024,x,6,x,6...
c600,5 ,2024,6,x,6,x..
I mean how I control it to tell the user input goes to specific row and column and adds data automatically to days column.
Here's an attempt to answer the question with the vague requirements. I will update if more information becomes available. Comment if anything is unclear.
To write rows with the csv
module, make a list of the column items. No need to add your own commas. In this case, make sure the header is a list of 34 items and each row of data is a list of 34 items. The csv
module will convert items in the lists to their string representation before writing them to a file, so you can use any object (int, float, string, etc.) in the lists.
import csv
import datetime as dt
FRIDAY = 4
# 34 columns total indexed 0-33
# The * expands the range in-place as 1,2,3...31
header = ['fingerprint', 'month', 'year', *range(1,32)]
year = int(input('Enter year: '))
month = int(input('Enter month: '))
with open('shoft.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(header)
for f in range(4):
finger = input('input the fingerprints: ')
# How 6,x order was determined wasn't specified.
# I chose alternating the output
if f % 2:
d1, d2 = 6, 'x'
else:
d1, d2 = 'x', 6
# Again, * expands [d1,d2]*15 in-place.
# e.g. 6,x,6,x...6 or x,6,x,6...x
# creates a length 34 list for row.
row = [finger, month, year, *([d1, d2] * 15), d1]
# Make all Fridays an x
for day in range(1, 31):
if dt.datetime(year, month, day).weekday() == FRIDAY:
# Day 1 is column 3, Day 2 is column 4, etc.
row[day + 2] = 'x'
writer.writerow(row)
Output:
Enter year: 2024
Enter month: 8
input the fingerprints: c100
input the fingerprints: c200
input the fingerprints: c300
input the fingerprints: c400
shoft.csv:
fingerprint,month,year,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
c100,8,2024,x,x,x,6,x,6,x,6,x,6,x,6,x,6,x,x,x,6,x,6,x,6,x,6,x,6,x,6,x,x,x
c200,8,2024,6,x,6,x,6,x,6,x,x,x,6,x,6,x,6,x,6,x,6,x,6,x,x,x,6,x,6,x,6,x,6
c300,8,2024,x,x,x,6,x,6,x,6,x,6,x,6,x,6,x,x,x,6,x,6,x,6,x,6,x,6,x,6,x,x,x
c400,8,2024,6,x,6,x,6,x,6,x,x,x,6,x,6,x,6,x,6,x,6,x,6,x,x,x,6,x,6,x,6,x,6
Fridays in 8/2024 are days 2, 9, 16, 23, and 30: