python - How can I transform a list into a csv? -


I get these values ​​which postgrescill (I has changed the color of the rows to a SQL query so that it can show that fert value is the same)

  ** ( '80255', 'GRN', decimal ( '4.00000000000000000000')) ** ( '80425', 'GRN', decimal ( '1.0000000000000000000000')) * * ( '80255', 'BCN', decimal ( '1.00000000000000000000')) ** ( '80425', 'BCN' decimal ('22 .0000000000000000 ')) ** (' 80255 ',' PT ', decimal ('2.0000000000000000')) ** ('80425', 'PT', Decimal ('5.0000000000000000')) ...  

I want to change next to a CSV file I am

  80255; 4; 1; 2 80425; 1; 22; 5  

In continuing with @Berney's good answer, there is a difference with more comments (GRN / BCN / PT)

< Div class = "post-text" itemprop = "text" is not dependent on line order, and it can deal with missing values:

  # Import archive and compact I import import as co imports as imports DC as import Sisivi fromdb = [( '80255', 'GRN', dc.Decimal ( '4.0000000000000000000000')), ( '80425', 'GRN', dc.Decimal ( '1.0000000000000000000000' '80255', ' BCN 'Diseedikmel' ( '1.00000000000000000000')), ( '80425', 'BCN' Diseedikmel ('22 K0000000000000000 ')), (' 80255 ',' PT ',' Disiakl '(2.0000000000000000')) , ( '80425', 'PT', dc.Decimal ( '5.0000000000000000')) # Create a dictionary that maps values ​​such as "80255" in the dictionary of values ​​for the GRN, BCN and PT tocsv = Kokdifolt Dikant ( For lambda: {'GRN': 0, 'BCN': 0, 'PT': 0}) I in indb: # Array index -1 refers to the last element # For example, set 'tocsv [' 80255 '] [' GRN '] from 4.00 KOCS [0]] [I [1]] = i [-1] with Open ('output.txt', 'wb') as f: CW = Csv.writer (f, delimiter = ';') # The item (key) and value (V) gives a notch of pairs # For the key '80255', the value will be 'GRN': 4, BCN ': 1,' PT ': 2} For Kashmir, V tocsv.ite (ms): # writerow () takes an array argument, therefore [] square bracket cw.writerow ([k, v ['gran'], v ['bcn'], v ['pt' ]]  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -