Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading from a file
#1
hi im currently working on a small project to make a bill bored chart and its on and we have to read the artists from a "csv" file we cannot use "import csv" methords and im still in the basic structure of reading the csv file but when i input the name and the delimiter it does not print in the array it just prints blank array this is what i have done so far
code
userFileName=input("Input the file name :- ")
userDelimiter=input("input the delimiter :- ")
userArray=[]

def get_csv_as_table(filename, delimiter):
    csvFile=open(filename)
    for row in csvFile:
        #row=row.strip()
        row=row.split(delimiter)
    userArray.append(row)
    print(userArray)

get_csv_as_table(userFileName, userDelimiter)
Reply
#2
You have a problem with your indentation on line 10. You are only appending the last row read to userArray instead of appending each row as the loop is processed.
Reply
#3
Also you have a scope of variable problem. Your append is not appending to the userArray you define outside of the function, rather it appends to one inside the function which is lost when you leave the function. That's actually why you get a blank array and not even that last row. Change your print in line 11 to
return userArray
, and then make the line 13 call
userArray = get_csv_as_table(userFileName, userDelimiter)
Then try printing userArray, or doing whatever you need with it.
Also, fix the indentation as above and please close the file.
Reply
#4
(Jul-13-2020, 08:08 PM)jefsummers Wrote: Also you have a scope of variable problem.

I was thinking the same thing, but a quick test of the code as written (with the indentation of line 10) revealed otherwise. I get a little confused by this, but I guess the code works in this case because the variable userArray is pointing to a list rather than storing a value of an immutable data type. Either way, it is definitely best practice and less confusing to distinguish the global and local variables and return a value from the function as you suggest.
Reply
#5
(Jul-13-2020, 08:08 PM)jefsummers Wrote: Also you have a scope of variable problem.
def get_csv_as_table(filename, delimiter):
    csvFile=open(filename)
    for row in csvFile:
        #row=row.strip()
        row=row.split(delimiter)
        userArray.append(row)
    csvFile.close()
    return userArray
    
get_csv_as_table(userFileName, userDelimiter)
Output:
Input the file name :- top_100.csv input the delimiter :- , >>>
i still get this output now the array wont even print:(
Reply
#6
You don't do anything with the return value of the function on line 10. Did you intend to print it?
Reply
#7
(Jul-15-2020, 04:54 AM)ndc85430 Wrote: You don't do anything with the return value of the function on line 10. Did you intend to print it?
yes i do :) wanna print it
Reply
#8
So do that?
Reply
#9
(Jul-15-2020, 05:00 AM)ndc85430 Wrote: So do that?
i did now but i dont get the proper output :(
userFileName=input("Input the file name :- ")
userDelimiter=input("input the delimiter :- ")
userArray=[]


def get_csv_as_table(filename, delimiter):
    csvFile=open(filename)
    for row in csvFile:
        #row=row.strip()
        row=row.split(delimiter)
        userArray.append(row)
    csvFile.close()
    print(userArray)
    return userArray
    
get_csv_as_table(userFileName, userDelimiter)
Output:
Input the file name :- meow.csv input the delimiter :- % [['Cat', 'Â\xa05', 'Â\xa0Â\xa020', 'Â\xa0meow\n'], ['Dog', 'Â\xa020', 'Â\xa020', 'Â\xa0woof\n'], ['Cow', 'Â\xa0300', 'Â\xa022', 'Â\xa0moo']
but the output i want is :( this
Output:
[[“Cat”, 5, 20, “meow”], [“Dog”, 20, 20, “woof”], [“cow”, 300, 22, “moo”]] 
what am i doing wrong?
Reply
#10
You aren't doing anything wrong, you just have more work to do. The Â\xa0 is a control character for a space. You could probably get rid of all of them by using the replace() method on each string, or you may need to look into using the unicodedata module or something similar. Also, if you want the numeric values to have a type other than string, you'll need to convert them to int or float as needed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel File reading vanjoe198 1 2,039 Mar-31-2021, 11:53 AM
Last Post: snippsat
  Weird problem with reading from file and performing calculations pineapple999 1 3,005 Jul-25-2019, 01:30 AM
Last Post: ichabod801
  Handling IO Error / Reading from file Expel 10 4,848 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,877 May-17-2019, 12:15 PM
Last Post: MvGulik
  reading text file and writing to an output file precedded by line numbers kannan 7 10,409 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Reading of structured .mat (matlab) file sumit 2 3,420 May-24-2018, 12:12 PM
Last Post: sumit
  File Reading toxicxarrow 9 5,184 May-07-2018, 04:12 PM
Last Post: toxicxarrow
  reading all lines from a text file seadoofanatic 2 2,926 Mar-13-2018, 06:05 PM
Last Post: Narsimhachary
  Reading a text file fivestar 7 5,591 Oct-13-2017, 07:25 AM
Last Post: gruntfutuk
  Dictionary + File Reading palmtrees 2 4,875 Nov-15-2016, 05:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020