0

i made this small program to read whats inside a comma separated file (exel)and it works but i get few additional characters i need help to fix it :(

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)`

and the output i get is

Input the file name :- meow.csv
input the delimiter :- %
[['Cat', 'Â\xa05', 'Â\xa0Â\xa020', 'Â\xa0meow\n'], ['Dog', 'Â\xa020', 'Â\xa020', 'Â\xa0woof\n'], ['Cow', 'Â\xa0300', 'Â\xa022', 'Â\xa0moo']

the output i want to get is

Input the file name :- meow.csv

input the delimiter :- %

[[“Cat”, 5, 20, “meow”], [“Dog”, 20, 20, “woof”], [“cow”, 300, 22, “moo”]]
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
  • Did you try `strip` while reading from the file? – DineshKumar Jul 15 '20 at 14:41
  • have you considered using the `csv` package built-in to python? – M Z Jul 15 '20 at 14:41
  • Does this answer your question? [How to remove the â\xa0 from list of strings in python](https://stackoverflow.com/questions/53222476/how-to-remove-the-%c3%a2-xa0-from-list-of-strings-in-python) – patmcb Jul 15 '20 at 14:42
  • @DineshKumar i tried i dont know exactly how to use it in this case – LooseCannon Jul 15 '20 at 14:53
  • @MZ i have to do it without using csv :( sorry for taking your time and thank you – LooseCannon Jul 15 '20 at 14:54
  • @patmcb they use extra functions that i havent used before its better as a beginner if i stay away from them for the timebeing right? – LooseCannon Jul 15 '20 at 14:55
  • @LooseCannon See my answer. All they're doing is adding "replace" to remove those characters. In their case, they're doing it with list comprehension. Here, you can just do it to the string. – patmcb Jul 15 '20 at 14:57

3 Answers3

1

Python has a built in csv module that should take care of those issues.

Torben545
  • 97
  • 1
  • 9
  • i have to do this without using csv sadly :( – LooseCannon Jul 15 '20 at 14:51
  • ...and why is that? csv is a built-in module, there is virtually no reason to not use it. – Tomalak Jul 15 '20 at 14:55
  • @Tomalak: there are many reasons: better error handling (and reporting to user), special dialect, mixed encoding (yeah there are such horrible files, especially exported from db). And in past we had bad experiences with csv (unicode/earlier version of Python 3). And it is very instructive to learn about CSV and how to encode it (and dialects). Only by programming we get it, so we understand better csv files (I already said that there are too many dialects?) – Giacomo Catenazzi Jul 15 '20 at 15:09
0

Below process will fetch required output:

            #Input file
            [[“Cat  , 5, 20, “meow”]% [“Dog”, 20, 20, “woof”]% [“cow”, 300, 22, “moo”]]


            #Program
            import csv
            def csv_reader1(file_obj):
                reader = csv.reader(file_obj,delimiter='%')
                for row in reader:
                    print(" ".join(row))

            if __name__ == "__main__":
                csv_path = "file3.csv"
                with open(csv_path, newline='',encoding="utf8") as f_obj:
                    csv_reader1(f_obj)
sudhirkondle
  • 127
  • 1
  • 5
0

If you really can't use the csv module for some reason, it seems like your current output is already very close to what you want. You just need to strip away the non-ASCII characters. I found this other answer that talks about how to do that:
How can I remove non-ASCII characters but leave periods and spaces using Python?

(I am also a new contributor.)