-1

I have a CSV File with a list of file names with no file extension. I have all the names as strings and would like to add a string .JPG to each line.

Here is what I have so far:

#Adding .JPG string to the end of each line for the Part Numbers
string_to_add = ".JPG"

#Open the file and join the .JPG to the current lines
with open("PartNums.csv", 'r') as f:
file_lines = [#Adding String goes here]

#Writes to the file until its done
with open("PartNums.csv", 'w') as f:
    f.writelines(file_lines) 

I think I need to use the .join function however I am not sure how to.

This is what the CSV file looks like now:

filename
filename
filename
filename
filename
filename

The output I am looking for is:

filename.JPG
filename.JPG 
filename.JPG
filename.JPG
filename.JPG
filename.JPG

I appreciate all and any help given, Thank you.

3
  • No, I would just like to add a string to a string from a CSV file. is there anyway I could do that? Commented Jul 1, 2022 at 16:52
  • 1
    JPG (assuming you refer to image format) is not a string. Commented Jul 1, 2022 at 16:58
  • I dont need it as a file extension Id like to add it as a string. I will be compare it to another list of string that look the same but have .JPG at the end so I need them to look the same Commented Jul 1, 2022 at 17:03

1 Answer 1

1

Okay, your question is a bit messy, but from your code, it seems like you have your filenames as a dataframe before you print it to csv. So if you would like to add'.JPG' to each filename it could be done before saving to csv and look something like this:

df = pd.DataFrame(['X00TB0001',
    'X01BJ0003',
    'X01BJ0004',
    'X01BJ0005',
    'X01BJ0006'], columns = ['jpgs'])
df['jpg_ext'] = df['jpgs'] + '.JPG'
df

just made it an extra column for clarity :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.