I am trying to build a ticketing system in python, and the output should read the file column by column. However the program is misreading the columns. Any help would be greatly appreciated. Thank you
This is the expected output:
Name Age Sex
as 12 f
qw 13 m
Total Tickets: 2
And this is the current output:
Name Age Sex
as qw
12 13
f m
Total Tickets: 2
I am trying to create and write to a csv file and then read from it. However, as shown with the ouputs above, I am getting the opposite of the desired output
The code is as follows:
import sys, select, os, csv
from os import system
with open(input("\nInput file name with .csv extension: "), 'r', newline = '') as f:
fileDir = os.path.dirname(os.path.realpath('__file__'))
reader = csv.reader(f, delimiter = '\t')
header_row = next(reader)
# Can use the 2 lines below to check indices of headers
# name = [0], age = [1], sex = [2]
# for index, column_header in enumerate(header_row):
# print(index, column_header)
name_l = []
age_l = []
sex_l = []
total_tickets = 0
for row in reader:
name = str(row[0])
name_l.append(name)
age = str(row[1])
age_l.append(age)
sex = str(row[2])
sex_l.append(sex)
total_tickets = 1
print('\t'.join(header_row))
print('\t'.join(name_l))
print('\t'.join(age_l))
print('\t'.join(sex_l))
print("\nTotal Tickets: ", total_tickets)
CodePudding user response:
Okay managed to figure it out for myself. So the new code is as follows:
import sys, select, os, csv
from os import system
with open(input("\nInput file name with .csv extension: "), 'r', newline = '') as f:
fileDir = os.path.dirname(os.path.realpath('__file__'))
reader = csv.reader(f, delimiter = '\t')
header_row = next(reader)
print('\t'.join(header_row))
# Can use the 2 lines below to check indices of headers
# name = [0], age = [1], sex = [2]
# for index, column_header in enumerate(header_row):
# print(index, column_header)
name_l = []
age_l = []
sex_l = []
total_tickets = 0
for row in reader:
name = str(row[0])
name_l.append(name)
age = str(row[1])
age_l.append(age)
sex = str(row[2])
sex_l.append(sex)
total_tickets = 1
print('\t'.join(row))
print("\nTotal Tickets: ", total_tickets)
CodePudding user response:
I see you going through some steps that are unnecessary, and even caused the confusion that lead to your original problem.
Here's a minimal example that asks for user input, reads in and counts the rows, and then writes everything out:
import csv
import sys
fname = input('\nInput file name with .tsv extension: ')
total_tickets = 0
rows = []
with open(fname, newline='') as f:
reader = csv.reader(f, delimiter='\t')
rows.append(next(reader)) # read and add header
for row in reader:
rows.append(row)
total_tickets = 1
writer = csv.writer(sys.stdout, delimiter='\t')
writer.writerows(rows)
print(f'\nTotal tickets: {total_tickets}')
Here's me running this:
% python3 so.py
Input file name with .tsv extension: so.tsv
Name Age Sex
as 12 f
qw 13 m
Total tickets: 2
The big takeaway is that there are two steps you can completely avoid:
string-converting all the cells is redundant, it's text coming in and text going out; if you just want to pass the data through, just append the whole row to
rows
, because...writing the rows manually can be tricky as you saw, and
csv.writer
can do it for you
I reviewed the Python docs answering this, and I can see that there is no complete example where tabulated data is read in, processed, and then written out, so I can see how you got the idea to write your rows yourself.
Also, I'd never seen newline=''
, and was scratching my head till I saw the docs mention thats the way to do it... learned something new, thanks!