简体   繁体   English

使用python将三个字符串连接到一个块中

[英]join three strings into one block using python

I have three strings. 我有三个弦。 Each string has names, adresses and postal code. 每个字符串都有名称,地址和邮政编码。

First string: 第一个字符串:

Joao Miesler
Bill Gates
Kevin Mitnick

Second string: 第二个字符串:

Street kit number 3
Street foo number 2
Street OOk number 4

Third string: 第三串:

2415-568
1452-856
1234-568

I would like the output to be something like: 我希望输出是这样的:

Joao Miesler
Street kit number 3
2415-568

Bill Gates
Street foo number 2
1452-856

Kevin Mitnick
Street OOk number 4
1234-568

This information was extracted from an excell worksheet like this: 此信息是从excell工作表中提取的,如下所示:

#Get columns name
for i in range(13, high_row, 1):
    nome = sheet['E%s' % i].value


#Get column adresses
for i in range(13, high_row, 1):
    morada = sheet['F%s' % i].value

#Get Postal Code column
for i in range(13, high_row, 1):
    cp = sheet['G%s' % i].value

Can you point me in the right direction? 你能为我指出正确的方向吗? Some light at the end of the tunnel would be nice. 隧道尽头的光线会很好。

After this I would like to make an A4 paper and divide the output into squares. 之后,我想制作一张A4纸并将输出分成正方形。 Any good library I can use to make it happen? 我有什么好的图书馆可以用来实现它?

Assuming your data is in multi-line strings: 假设您的数据位于多行字符串中:

names = """Joao Miesler
Bill Gates
Kevin Mitnick"""

the Pythonic way to do this is to operate on lists of lines and join them using zip : 使用Python的方法是对行列表进行操作,并使用zip将它们加入:

names = names.split('\n')
addresses = addresses.split('\n')
phone_numbers = phone_numbers.split('\n')

for name, address, phone in zip(names, addresses, phone_numbers):
   print('%s\n%s\n%s\n' % (name, address, phone))

If you use lists to store each line of the 3 strings either separately or together the you can choose which lines to print with basic list functionality. 如果使用列表分别或一起存储3个字符串的每一行,则可以选择要使用基本列表功能打印的行。

So it would look like: 因此,它看起来像:

list1 = ['Joao Miesler', 'Bill Gates', 'Kevin Mitnick']
list2 = ['Street kit number 3', 'Street foo number 2', 'Street OOk number 4']
list3 = ['2415-568', '1452-856', '1234-568']

print(list1[0],\n,list2[0],\n,list3[0])

etc 等等

Why have three loops when you could just have one? 为什么只有三个回路就只有三个回路?

for i in range(13, high_row, 1):
    nome = sheet['E%s' % i].value
    morada = sheet['F%s' % i].value
    cp = sheet['G%s' % i].value

    data = [nome, morada, cp]
    print('\n'.join(map(str,data)))

Assuming those are multi line strings divided by \\n and all have the same length 假设这些是多行字符串除以\\n并且长度相同

s1a = s1.split('\n')
s2a = s2.split('\n')
s3a = s3.split('\n')

newString = '' 
for i in range(len(s1a)):
    newString += (s1a[i]+'\n')
    newString += (s2a[i]+'\n')
    newString += (s3a[i]+'\n')
    newString += '\n'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM