SageTV Community  

Go Back   SageTV Community > General Discussion > General Discussion
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

General Discussion General discussion about SageTV and related companies, products, and technologies.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-28-2023, 10:34 AM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
A little Python Help....

I am having a brain fart... this should be so easy but....

I have a text file that I would like to add an incremental number to the end of each instance for example of the word "STUDENT:

sudent.txt
blah blah blah STUDENT001 next Instance STUDENT002 etc and so on.
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 04-28-2023 at 11:35 AM.
Reply With Quote
  #2  
Old 04-29-2023, 09:22 AM
wayner wayner is offline
SageTVaholic
 
Join Date: Jan 2008
Location: Toronto, ON
Posts: 7,491
Try using ChatGPT from the Bing search engine. I asked it "how do you loop through numbers 1 to 10 in python and add the number to the word "student""

Code:
You can loop through numbers 1 to 10 in Python using a for loop and add the number to the word “student” by typecasting the number into a string using typecasting and then concatenating using the ‘+’ operator. Here’s an example:

for i in range(1, 11):
    text = "student " + str(i)
    print(text)
This will output:

student 1
student 2
student 3
student 4
student 5
student 6
student 7
student 8
student 9
student 10
I hope this helps! Let me know if you have any other questions.
__________________
New Server - Sage9 on unRAID 2xHD-PVR, HDHR for OTA
Old Server - Sage7 on Win7Pro-i660CPU with 4.6TB, HD-PVR, HDHR OTA, HVR-1850 OTA
Clients - 2xHD-300, 8xHD-200 Extenders, Client+2xPlaceshifter and a WHS which acts as a backup Sage server
Reply With Quote
  #3  
Old 04-29-2023, 10:23 AM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Quote:
Originally Posted by wayner View Post
Try using ChatGPT from the Bing search engine. I asked it "how do you loop through numbers 1 to 10 in python and add the number to the word "student""

Code:
You can loop through numbers 1 to 10 in Python using a for loop and add the number to the word “student” by typecasting the number into a string using typecasting and then concatenating using the ‘+’ operator. Here’s an example:

for i in range(1, 11):
    text = "student " + str(i)
    print(text)
This will output:

student 1
student 2
student 3
student 4
student 5
student 6
student 7
student 8
student 9
student 10
I hope this helps! Let me know if you have any other questions.
Wow that is amazing can this be done with a c:\test\student.txt file as input ? with multiple instances of student.
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 04-29-2023 at 10:25 AM.
Reply With Quote
  #4  
Old 04-29-2023, 10:38 AM
wayner wayner is offline
SageTVaholic
 
Join Date: Jan 2008
Location: Toronto, ON
Posts: 7,491
Not sure exactly what you are asking for, does this work:

Code:
You can loop through a text file called “student.txt” and replace each instance of “STUDENT” with “STUDENT1” where the number is incremented by using a for loop and the replace() function1. Here’s an example:

with open('student.txt', 'r') as f:
    lines = f.readlines()

with open('student.txt', 'w') as f:
    for i, line in enumerate(lines):
        line = line.replace('STUDENT', f'STUDENT{i+1}')
        f.write(line)
This will replace each instance of “STUDENT” with “STUDENT1”, “STUDENT2”, etc.
__________________
New Server - Sage9 on unRAID 2xHD-PVR, HDHR for OTA
Old Server - Sage7 on Win7Pro-i660CPU with 4.6TB, HD-PVR, HDHR OTA, HVR-1850 OTA
Clients - 2xHD-300, 8xHD-200 Extenders, Client+2xPlaceshifter and a WHS which acts as a backup Sage server
Reply With Quote
  #5  
Old 04-29-2023, 10:42 AM
nyplayer nyplayer is offline
SageTVaholic
 
Join Date: Sep 2005
Posts: 4,997
Quote:
Originally Posted by wayner View Post
Not sure exactly what you are asking for, does this work:

Code:
You can loop through a text file called “student.txt” and replace each instance of “STUDENT” with “STUDENT1” where the number is incremented by using a for loop and the replace() function1. Here’s an example:

with open('student.txt', 'r') as f:
    lines = f.readlines()

with open('student.txt', 'w') as f:
    for i, line in enumerate(lines):
        line = line.replace('STUDENT', f'STUDENT{i+1}')
        f.write(line)
This will replace each instance of “STUDENT” with “STUDENT1”, “STUDENT2”, etc.
That is exactly what I was asking for Thanks That AI is great playing around with it.

It worked Thank you
__________________
Channels DVR UBUNTU Server 2 Primes 3 Connects TVE SageTV Docker with input from Channels DVR XMLTV and M3U VIA Opendct.

Last edited by nyplayer; 04-29-2023 at 10:53 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Python help .... nyplayer General Discussion 19 02-22-2022 12:05 AM


All times are GMT -6. The time now is 11:36 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.