-1

I am trying to concatenate a string to send a message via python>telegram My plan is so that the function is modular. It first import lines from a .txt file and based on that many lines it creates two different arrays array1[] and array2[], array1 will receive the values of the list as strings and array2 will receive user generated information to complemente what is stored in the same position as to a way to identify the differences in the array1[pos], as to put in a way:

while (k<len(list)):

array2[k]= str(input(array1[k]+": "))

k+=1

I wanted to create a single string to send in a single message like however in a way that all my list goes inside the same string

string1 = array1[pos]+": "+array2[pos]+"\n"

I have tried using while to compared the len but I kept recalling and rewriting my own string again and again.

  • This question is very hard to follow. Can you be more specific as to what you're trying to do? Where are you loading in the file? Is the file a list? What should your file look like? – spen.smith Mar 22 '22 at 01:24
  • I'll try to make it short. – nickksoares Mar 25 '22 at 22:22
  • So at first I am importing several strings contained in a TXT file. From every single line I am creating a attributed "array" and an empty array with the corresponding positions, so ` ArrayWithoutAnything[0] ` would need to concatenate with ` ArrayWithSomething[0] ` and so on. However since I need to send the concatenated String via a Telegram Message I would need to create a formula to make so that every position inside the array would concatenate accordingly indepedently if I have [n] positions. – nickksoares Mar 25 '22 at 22:28
  • Thanks Nick. I'll post something, let me know if it helps. – spen.smith Mar 26 '22 at 23:07

2 Answers2

0
list_of_strings_from_txt = ["A","B","C"]
modified_list = [f"{w}: {input(f'{w}:')}" for w in list_of_strings_from_txt]

I guess? maybe?

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

It looks like what you're looking for is to have one list that comes directly from your text file. There's lots of ways to do that, but you most likely won't want to create a list iteratively with the index position. I would say to just append items to your list.

The accepted answer on this post has a good reference, which is basically the following:

import csv

with open('filename.csv', 'r') as fd:
    reader = csv.reader(fd)
    for row in reader:
        # do something

Which, in your case would mean something like this:

import csv

actual_text_list = []
with open('filename.csv', 'r') as fd:
    reader = csv.reader(fd)
    for row in reader:
        actual_text_list.append(row)

user_input_list = []
for actual_text in actual_text_list:
    the_users_input = input(f'What is your response to {actual_text}? ')    
    user_input_list.append(the_users_input)

This creates two lists, one with the actual text, and the other with the other's input. Which I think is what you're trying to do.


Another way, if the list in your text file will not have duplicates, you could consider using a dict, which is just a dictionary, a key-value data store. You would make the key the actual_text from the file, and the value the user_input. Another technique, you could make a list of lists.

import csv

actual_text_list = []
with open('filename.csv', 'r') as fd:
    reader = csv.reader(fd)
    for row in reader:
        actual_text_list.append(row)

dictionary = dict()
for actual_text in actual_text_list:
    the_users_input = input(f'What is your response to {actual_text}? ')    
    dictionary[actual_text] = the_users_input

Then you could use that data like this:

for actual_text, user_input in dictionary.items():
    print(f'In response to {actual_text}, you specified {user_input}.')

spen.smith
  • 576
  • 2
  • 16
  • I guess i`ll try using csv. As you mentioned last There are no duplicates. It will always be Letters+numbers, as to assign multiple vehicles with their "callsign" as to speak. So I have UR 11199 and UR 11198, I could still use spaces inside dict? Maybe Not. – nickksoares Mar 28 '22 at 19:44
  • But the first part Im thinking of ways to implement using CSV, the thing is that Im not the one filling the the necessary info, so I would like to be the most anti-dumb as possible. – nickksoares Mar 28 '22 at 19:45