5

I am trying to write pandas df to an output file with:

outFileName="myfile_" + gene_name #gene_name gets a new value from argparse 

df.to_csv('%.csv' %outFileName, sep=',', index=False)

the error I am getting::

TypeError: %c requires int or char

I tried

outFileName=str("myfile_" + gene_name)

and got the same error. Am I missing something? I can save the df if I do not use variable as outputfile name but I need to have the variable.

3 Answers 3

5

Use to_csv:

df.to_csv(outFileName+".csv", sep=',', index=False)
Sign up to request clarification or add additional context in comments.

Comments

1

I think you can use format for new name for file:

df.to_csv('myfile_{}.csv'.format(gene_name), sep=',', index=False)

Or if use python 3.5+ is possible use f-strings:

df.to_csv(f'myfile_{gene_name}.csv', sep=',', index=False)

Comments

1
name = x
nameFile = name +'.csv'

with open(nameFile, "a") as a_file:
    a_file.write(data)

1 Comment

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.