How to print/insert in an organized way inside a csv file a tuple in Python

Asked

Viewed 46 times

3

Would anyone know how to print a tuple as follows:

  amount_of_commits,author  
  148, Steve Ellis     
  2, Alex Kwiatkowski   
  2, Thomas  
  1, Dan Forbes  
  1, David Parker  
  1, John Barker  
  1, Thomas Hodges  
  1,dependabot[bot]  

The return I’m getting is from command git shortlog -s -n whose code is below:

for token in tokens:
insights = open("repositories_insights/" + token + "_insights.csv", "w")
insights.write("amount_of_commits,author\n")
   
data = subprocess.Popen(["git", "-C", "github_repos/" + token + "/" + token, "shortlog",  "-s", "-n"], stdout=subprocess.PIPE).communicate()
print(str(data))
insights.write(data)
insights.close()

When printing tuple value data the result is as follows:

(b'   148\tSteve Ellis\n     2\tAlex Kwiatkowski\n     2\tThomas\n     1\tDan Forbes\n     1\tDavid Parker\n     1\tJohn Barker\n     1\tThomas Hodges\n     1\tdependabot[bot]\n', None)

1 answer

2


According to the documentation of communicate the return is a tuple containing the standard output and error output. Also, when it is not in text mode, the return is an object of type bytes (can you see by b at the beginning of the output). Also, since the command gave no error, the second element of the tuple is None.

So you need to take the first element of the tuple (which is the default output of the command) and turn it into a string, using the method decode.

Also, the command is returning the fields separated by a TAB (\t), then just use replace to replace it with a comma:

data = subprocess.Popen(["git", "-C", "github_repos/" + token + "/" + token, "shortlog",  "-s", "-n"], stdout=subprocess.PIPE).communicate()
insights.write(data[0].decode().replace('\t', ', '))

Another option is to read the stdout process from row to row:

import subprocess

# atenção, aqui não chama communicate()
data = subprocess.Popen(["git", "-C", "github_repos/" + token + "/" + token, "shortlog",  "-s", "-n"], stdout=subprocess.PIPE)
for s in data.stdout:
    insights.write(s.decode().replace('\t', ', '))

Another option is to set the argument universal_newlines, so the return will be strings instead of bytes, eliminating the need to call decode:

data = subprocess.Popen(["git", "-C", "github_repos/" + token + "/" + token, "shortlog",  "-s", "-n"], stdout=subprocess.PIPE, universal_newlines=True).communicate()
insights.write(data[0].replace('\t', ', '))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.