-1

So as an example the data I'm using in a .csv file is

NA,ShimmyXD,#NA1,Radiant,135.8,992,24.9,0,140,80,161,"1,506","1,408",703,1.07,0.7,29,208.8,59,59.6,Fade,Viper,Omen,Vandal,35,59,5,802,Phantom,33,62,5,220,Classic,36,60,3,147

I'm currently trying to split it in python and removing all of the commas however when I use the split it turns "1,506" -> '"1', '506"' which I don't want I want it to split everything but keep it as 1,506 or 1506

Currently, this is how my code looks like is not final it's still a WIP

def ValorantDoL(file):
    """
    Sorts data into Dictionary of List
    """
    Data = {}
    f = open(file, 'r', encoding='UTF-8') #encoding allows us to get everything in the file
    headers = f.readline().strip().split(',')
    Data[headers[0]] = []
    for num, line in zip(range(len(headers)),f):
        line = line.strip().split(',') #<-- Issue Here
        print(line)
        print("#---------------------#")
        Data[headers[num]] = (line[num])
        
    f.close()
    return Data

When I run this it returns

['NA', 'ShimmyXD', '#NA1', 'Radiant', '135.8', '992', '24.9', '0', '140', '80', '161', '"1', '506"', '"1', '408"', '703', '1.07', '0.7', '29', '208.8', '59', '59.6', 'Fade', 'Viper', 'Omen', 'Vandal', '35', '59', '5', '802', 'Phantom', '33', '62', '5', '220', 'Classic', '36', '60', '3', '147']

Any idea what the problem is? I tried using replace function but that doesn't seem to work either.

I just want it to return this

['NA', 'ShimmyXD', '#NA1', 'Radiant', '135.8', '992', '24.9', '0', '140', '80', '161', '"1,506"', '"1,408"', '703', '1.07', '0.7', '29', '208.8', '59', '59.6', 'Fade', 'Viper', 'Omen', 'Vandal', '35', '59', '5', '802', 'Phantom', '33', '62', '5', '220', 'Classic', '36', '60', '3', '147']

Andyrew
  • 13
  • 4

1 Answers1

-2

Alright, @mozway and @Tim Biegeleisen you guys are a freaking lifesaver. I didn't know I could parse with CSV until today

The fixed code is

def ValorantDoL(file):
    """
    Sorts data into Dictionary of List
    """
    Data = {}
    with open(file, 'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        
        header = next(csv_reader) #grabs header

        for line in csv_reader: #Grabs lines without needing to use split or strip
            print(line)
        
    f.close()
    return Data

With this, I can grab the respect variables and store them within my Dictionary respectively as I write the rest of the code. But so far this fixes my issue

Andyrew
  • 13
  • 4