-1

I am novice with Python coding. I am having some issue to send data file into CSV file. I am not sure why it's not being sent the way it's being printed. I couldn't manage to correct way looping. What did I miss in the code?

Image 1: shows how newline is being printed on right manner. However, when I use the for loop down it's sending the file like image 2. What did I mistake and how to resolve to send exactly the same as showing into print data?

Image:1

enter image description here

Image:2

enter image description here

Image:3

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
KRMNDev
  • 13
  • 4

1 Answers1

1

the variable 'newfile' in your first for-loop keeps getting overwritten each time your loop through it at line 52. Finally when you exit the loop and enter a new loop at line 72, you are iterating over the LAST assignment set on the 'newfile' variable (see https://stackoverflow.com/a/1816897/5198805) . You're also opening the same file multiple times and ovewriting it. You should open it once and then keep writing each line.

You should save the values in a temporary list

my_lines = []
#line 52
for row_in in range(len(..... stuff)):
    
    ... your code

    #line 69
    print(newfile)  # <-- this could be called processed_csv_line
    # new code you need to add here
    processed_csv_line = newfile
    my_lines = my_lines.append(processed_csv_line)


output_file = open("_output.csv","w")
csv_writer = csv.writer(output_file)
# line 72
for row_n in my_lines:
     csv_writer.writerow(row_n)

#remember to close file after writing
output_file.close()
Ryu S.
  • 1,538
  • 2
  • 22
  • 41
  • I am having another issue to connect Analysis server from python. Currently i can connect local version of AnalysisService.Tabular.dll file from root = Path(r"C:\Windows\Microsoft.NET\assembly\GAC_MSIL") . However, as it will be used in cloud as a script so I need to use something platform independent. Any idea how i can make it platform independent? Using API or something else? Thanks again. – KRMNDev Mar 03 '22 at 14:15
  • image 3: is current working code. However, i need to get platform independent code. Can you please help me. Thank you – KRMNDev Mar 03 '22 at 15:03
  • @KRMNDev this is an entirely different question now, I suggest you close this one and complete a few tutorials to understand the analysis services platform. see: https://learn.microsoft.com/en-us/azure/analysis-services/tutorials – Ryu S. Mar 03 '22 at 20:23
  • If you MUST use it from python you could try this: https://github.com/yehoshuadimarsky/python-ssas – Ryu S. Mar 03 '22 at 20:31