I am generating random edges from a graph and printing them, but every time I try to save it to a text file, I end up with errors like TypeError: expected a character buffer object. The code is below:
import re
import md5
import pickle
import networkx as nx
import itertools
from random import choice
ff=open("testfile.txt","r+")
G=nx.read_gpickle("authorgraph_new.gpickle")
for nodes in G:
random_edge=choice(G.edges())
ff=open("testfile.txt","a")
ff.write(random_edge)
I need to save the outputs of random_edge in a text file preferably in columns, as the value of random_edge is in the form (abcdef, pqrstu). I want to put the two in separate column in the same line and the next value in the same columns. I know I can use the "\n" to get the outputs to newline but when I used
ff.write(random_edge + "\n")
I get an error TypeError: can only concatenate tuple (not "str") to tuple.
print(..., file=ff)instead offf.write(...). (I'm assuming you are using Python 3.x.)random_edgeis a tuple, "\n" is a string. What do you expect the result oftuple + stringto be?