Python 如何将字符串置于彼此之下并为其分配一个数字?

Python 如何将字符串置于彼此之下并为其分配一个数字?,python,python-2.7,Python,Python 2.7,我想改革这个 A,B AFD,DNGS,SGDH NHYG,QHD,lkd,uyete AFD,TTT 并为每一行分配一个数字,这将成为 A 1 B 1 AFD 2 DNGS 2 SGDH 2 NHYG 3 QHD 3 lkd 3 uyete 3 AFD 4 TTT 4 我怎样才能做到这一点 我被困在以下代码中: import itertools # open the data from the directory wit

我想改革这个

A,B                   
AFD,DNGS,SGDH         
NHYG,QHD,lkd,uyete    
AFD,TTT
并为每一行分配一个数字,这将成为

A 1
B 1
AFD 2
DNGS 2
SGDH 2
NHYG 3
QHD 3
lkd 3
uyete 3   
AFD 4
TTT 4
我怎样才能做到这一点

我被困在以下代码中:

import itertools
# open the data from the directory
with open ( './test.txt' ) as f:
    # Make a file named result to write your results there
    with open ( 'result.txt' , 'w' ) as w:
        # read each line with enumerate ( a number for each string)
        for n , line in enumerate ( f.readlines ( ) ):
with open('test.txt') as f_in:
    print '\n'.join('{} {}'.format(s, i)
                    for i, l in enumerate(f_in, 1)
                    for s in l.strip().split(','))

在文件的顶部包含这个函数,这样您就可以使用print函数了。在Python3中,print语句是不推荐使用的

from __future__ import print_function
那么您应该根据以下内容拆分该行:


在文件的顶部包含这个函数,这样您就可以使用print函数了。在Python3中,print语句是不推荐使用的

from __future__ import print_function
那么您应该根据以下内容拆分该行:

您可以使用以下命令:

with open('test.txt') as f_in, open('result.txt', 'w') as f_out:
    f_out.write('\n'.join('{} {}'.format(s, i)
                          for i, l in enumerate(f_in, 1)
                          for s in l.strip().split(',')))
在上面的示例中,将从输入文件返回索引、行元组,索引从作为参数传递的1开始。然后,for each line用于删除尾随换行符,然后,该行是从每个,。最后,生成器表达式输出格式为“单词索引”的字符串,这些字符串在写入输出文件之前与换行符连接在一起

如果要打印结果而不是写入文件,请使用以下代码进行更新:

import itertools
# open the data from the directory
with open ( './test.txt' ) as f:
    # Make a file named result to write your results there
    with open ( 'result.txt' , 'w' ) as w:
        # read each line with enumerate ( a number for each string)
        for n , line in enumerate ( f.readlines ( ) ):
with open('test.txt') as f_in:
    print '\n'.join('{} {}'.format(s, i)
                    for i, l in enumerate(f_in, 1)
                    for s in l.strip().split(','))
输出:

A 1
B 1
AFD 2
DNGS 2
SGDH 2
NHYG 3
QHD 3
lkd 3
uyete 3
AFD 4
TTT 4
您可以使用以下命令:

with open('test.txt') as f_in, open('result.txt', 'w') as f_out:
    f_out.write('\n'.join('{} {}'.format(s, i)
                          for i, l in enumerate(f_in, 1)
                          for s in l.strip().split(',')))
在上面的示例中,将从输入文件返回索引、行元组,索引从作为参数传递的1开始。然后,for each line用于删除尾随换行符,然后,该行是从每个,。最后,生成器表达式输出格式为“单词索引”的字符串,这些字符串在写入输出文件之前与换行符连接在一起

如果要打印结果而不是写入文件,请使用以下代码进行更新:

import itertools
# open the data from the directory
with open ( './test.txt' ) as f:
    # Make a file named result to write your results there
    with open ( 'result.txt' , 'w' ) as w:
        # read each line with enumerate ( a number for each string)
        for n , line in enumerate ( f.readlines ( ) ):
with open('test.txt') as f_in:
    print '\n'.join('{} {}'.format(s, i)
                    for i, l in enumerate(f_in, 1)
                    for s in l.strip().split(','))
输出:

A 1
B 1
AFD 2
DNGS 2
SGDH 2
NHYG 3
QHD 3
lkd 3
uyete 3
AFD 4
TTT 4

下面是一个小示例,展示了如何在Python中处理字符串拆分。请记住,这是用Python 3编写的,因此您必须将其改编为Python 2.7:

arr = ("A,B","AFD,DNGS,SGDH","NHYG,QHD,lkd,uyete","AFD,TTT")
for i in range(0,len(arr)):
    splitarr = arr[i].split(",")
    for splitItem in splitarr:
        print(splitItem + " " + str(i+1))

下面是一个小示例,展示了如何在Python中处理字符串拆分。请记住,这是用Python 3编写的,因此您必须将其改编为Python 2.7:

arr = ("A,B","AFD,DNGS,SGDH","NHYG,QHD,lkd,uyete","AFD,TTT")
for i in range(0,len(arr)):
    splitarr = arr[i].split(",")
    for splitItem in splitarr:
        print(splitItem + " " + str(i+1))

我正要发布我的答案,然后我看到了这个,真的很棒@niemmi我没有错误,但也没有输出?您的代码非常复杂,我无法理解,是否可以显示如何打印结果:-@nik输出被写入与您问题中同名的文件。查看更新后的答案,了解如何打印,以及对发生的情况的简短解释。@nik我在答案中尝试了两个版本,它们都按照我的预期工作。您是否尝试过使用与您发布的数据完全相同的数据和与我的答案完全相同的代码,而没有添加/更改/删除任何内容?test.txt是否与脚本位于同一文件夹中?@niemmi我喜欢并接受了你的答案。在Mac中保存有问题。当我将xls文件保存到txt时,它会添加到其中,并且当我将其发布到GitHub上时,您的代码运行良好,但在我的数据中它不会:-然而,非常感谢您的帮助我正要发布我的答案,然后我看到了这一点,非常整洁@niemmi我没有错误,但也没有输出?您的代码非常复杂,我无法理解,是否可以显示如何打印结果:-@nik输出被写入与您问题中同名的文件。查看更新后的答案,了解如何打印,以及对发生的情况的简短解释。@nik我在答案中尝试了两个版本,它们都按照我的预期工作。您是否尝试过使用与您发布的数据完全相同的数据和与我的答案完全相同的代码,而没有添加/更改/删除任何内容?test.txt是否与脚本位于同一文件夹中?@niemmi我喜欢并接受了你的答案。在Mac中保存有问题。当我将xls文件保存到txt时,它会添加到其中,并且当我将其发布到GitHub上时,您的代码工作正常,但在我的数据中它不会:-但是,非常感谢您的帮助我喜欢您的答案谢谢,但Line给出了错误,我不知道我的Mac电脑或我的编程有什么问题:-它让我crazy@nik您是否在程序的顶部包含了from _; u future _;行?您也可以使用print>>w,word,n+1,但您应该真正开始在所有程序中使用print函数,在2.7中,您可以使用该导入。这与使用Mac电脑无关。我喜欢你的回答,谢谢,但Line给出了错误,我不知道我的Mac电脑或我的编程有什么问题:-这让我crazy@nik您是否在程序的顶部包含了from _; u future _;行?您也可以使用print>>w,word,n+1,但您应该真正开始在所有程序中使用print函数,在2.7中,您可以使用该导入。这与在Mac上无关。您可以使用enumerate,而不是遍历范围并通过索引从数组中获取值。与此类似,对于索引,枚举arr中的值。您可以使用枚举,而不是遍历范围并按索引从数组中获取值。像这样的索引,枚举arr中的值。