1

I have a subject_id which is a dynamic number.For instance, it could be equal to 60. I am manually defining some file names as follows: x_file = "50.txt" x_csv_file = "50.csv" The number (50) could have been 1 or any-number else. Is there any way that I can define subject_id=50 JUST one time and then use those names as x_file = "subject_id.txt" and x_csv_file = "subject_id.csv"?. Thanks For your help

4
  • You'll probably want to save subject_id='50' and then do x_file=subject_id + '.txt' and x_csv_file=subject_id + '.csv: The + between two strings acts as the concatenation operator, hence the strings are pasted together to produce your desired output Commented Dec 5, 2019 at 21:52
  • Thank you for your help. But it gives me the following error:TypeError: unsupported operand type(s) for +: 'int' and 'str Commented Dec 5, 2019 at 21:58
  • 1
    You're probably missing the quotes around the number then Commented Dec 5, 2019 at 21:59
  • 1
    @LukasThaler Thanks Lukas. You are right! Thank you for your help Commented Dec 5, 2019 at 22:07

1 Answer 1

1

You might want to define a simple function for this

def file_name(subject_id):
    x_file = '{}.txt'.format(subject_id)
    x_csv_file = '{}.csv'.format(subject_id)
    return x_file, x_csv_file
Sign up to request clarification or add additional context in comments.

3 Comments

Worked as a charm!
You can do x_csv_file = str(subject_id) + '.csv' with Lukas' solution too : )
Why not use f-strings?

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.