У меня есть список с множеством вопросов. Я хочу, чтобы каждый из этих вопросов был go в отдельные файлы .txt. Как я могу это сделать? - PullRequest
0 голосов
/ 12 июля 2020

Это мой список. Я хочу, чтобы каждый элемент был в отдельном файле .txt

['What is Income tax?',
 'What is the period for which a person’s income is taken into account for the purpose of Income tax?',
 'What is Indirect Tax?',
 'What is the Goods and Service Tax (GST)?',
 'What is a composition scheme?',
 'Who can opt for a composition scheme?',
 'I have some queries regarding taxation of my business, can anyone help?',
 'Direct tax and indirect tax.',
 'How are small businesses taxed in India?',
 'information related to tax',
 'how do i calculate tax as per new rules']

1 Ответ

0 голосов
/ 12 июля 2020

Предполагая, что вы жестко кодируете операторы в виде списка в скрипте, который вы пытаетесь записать в python, вы можете сделать что-то вроде этого:

# Hard-coded statements
statements = ['statement1', 'statement2', ...] 

# Loop through all the statements
for index, statement in enumerate(statements):

    # Create a new text file with the index as the filename
    with open('{}.txt'.format(index), 'w') as output_file:

        # Write the statement into the text file
        output_file.write(statement)

Это автоматически использует индекс в качестве имени файла. Вы также можете указать имена файлов, если сохраните их в отдельном списке, например:

# Hard-coded statements
statements = ['statement1', 'statement2', ...] 

# Hard-coded filenames
filenames = ['statement1.txt', 'statement2.txt', ...]

# Loop through all the statements
for index, statement in enumerate(statements):

    # Create a new text file with the index as the filename
    with open(filenames[index], 'w') as output_file:

        # Write the statement into the text file
        output_file.write(statement)

Вы, вероятно, можете представить, что существует несколько различных способов go решения этой конкретной проблемы, но, надеюсь, это даст вы отправная точка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...