不同行之前/之后的Python打印

时间:2019-03-04 01:29:37

标签: python python-3.x bbcode

我很早以前就创建了一些代码,这些代码有助于在论坛中使用的BBcode中创建表格。

counter = 0
counter2 = 0
while True:
        UserInput = input("")
    if counter2 == 0:
        print ("[tr]")
    print ("[td][center]Label\n" + "[img]" + str(UserInput) + "[/img][/center][/td]")
    counter += 1
    counter2 += 1
    if counter % 5 == 0:
        print ("[/tr]")

因此,如果我在单独的行上输入Image1.jpg〜Image7.jpg,则输出如下所示

> [tr] 
> [td][center]Label[img]Image1.jpg[/img][/center][/td] 
> [td][center]Label[img]Image2.jpg[/img][/center][/td] 
> [td][center]Label[img]Image3.jpg[/img][/center][/td] 
> [td][center]Label[img]Image4.jpg[/img][/center][/td] 
> [td][center]Label[img]Image5.jpg[/img][/center][/td] 
> [/tr]
> [td][center]Label[img]Image6.jpg[/img][/center][/td] 
> [td][center]Label[img]Image7.jpg[/img][/center][/td] 

当前,该代码仅在每5张图像的末尾插入[/ tr]。无论输入多少jpg,如何制作[/ tr]也要在输出末尾打印? >

如何在开始处打印[tr]并将其与下面的行合并,然后在打印[/ tr]之前不再次打印?

为我的英语和口语技巧道歉。

(当前进度)

counter = 0
while True:
    UserInput = input("")
    if counter == 0 or counter % 5 == 0:
        print("[tr]", end = "")
    print ("[td][center]Label\n" + "[img]" + str(UserInput) + "[/img][/center][/td]")
    counter += 1
    if counter % 5 == 0:
        print("[/tr]")

3 个答案:

答案 0 :(得分:1)

阅读5次之后,我相信您想要的是:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new CupertinoApp(
    title: 'App Name',
    home: new RootPage(),
    routes: <String, WidgetBuilder>{
      '/home': (BuildContext context) => new Dashboard(),
      '/signin': (BuildContext context) => new LoginPage(),
    });
  }

因此,这里发生的是,您可以根据需要在内部从第一张打印的同一行打印[tr]。 [/ tr]位于换行符中,但是您也可以通过在第二个打印件中添加end =“”将其放在同一行中。

答案 1 :(得分:1)

分隔功能。获取图像列表,然后进行处理:

def bbcode(images):
    for i in range(0,len(images),5):
        print('[tr]')
        for image in images[i:i+5]:
            print(f'[td][center]Label[img]{image}[/img][/center][/td]')
        print('[/tr]')

def get_images():
    images = []
    while True:
        image = input('Image? ')
        if not image: break
        images.append(image)
    return images

images = get_images()
bbcode(images)

您可以将其作为一个长脚本来完成,但是并不清楚:

count = 0

while True:
    image = input('Image? ')
    if not image:
        break

    count = (count + 1) % 5
    if count == 1:
        print('[tr]')
    print(f'[td][center]Label[img]{image}[/img][/center][/td]')
    if count == 0:
        print('[/tr]')

if count != 0:
    print('[/tr]')

答案 2 :(得分:1)

下面是带有一些评论的结果。要更新您的规格,只需将max_item_blocks变量设置为所需的任何值即可。

### your main body element with {} to pass a number
element = '[td][center]Label[img]Image{}.jpg[/img][/center][/td]'

### The number of "blocks" you want to print.
max_item_blocks = 3

### Define a start value of 1
start = 1

### Our simple loop with join() function
while max_item_blocks > 0:

    ### End value is start + 5
    end = start + 5

    print('[tr]\n' + '\n'.join([element.format(i) for i in range(start, end)]) + '\n[\\tr]')

    ### Start takes ending value
    start = end

    ### Ending value is now start + 5
    end = start + 5

    ### Reduce our block counts by 1
    max_item_blocks -= 1

3个块的输出:

[tr]
[td][center]Label[img]Image1.jpg[/img][/center][/td]
[td][center]Label[img]Image2.jpg[/img][/center][/td]
[td][center]Label[img]Image3.jpg[/img][/center][/td]
[td][center]Label[img]Image4.jpg[/img][/center][/td]
[td][center]Label[img]Image5.jpg[/img][/center][/td]
[\tr]
[tr]
[td][center]Label[img]Image6.jpg[/img][/center][/td]
[td][center]Label[img]Image7.jpg[/img][/center][/td]
[td][center]Label[img]Image8.jpg[/img][/center][/td]
[td][center]Label[img]Image9.jpg[/img][/center][/td]
[td][center]Label[img]Image10.jpg[/img][/center][/td]
[\tr]
[tr]
[td][center]Label[img]Image11.jpg[/img][/center][/td]
[td][center]Label[img]Image12.jpg[/img][/center][/td]
[td][center]Label[img]Image13.jpg[/img][/center][/td]
[td][center]Label[img]Image14.jpg[/img][/center][/td]
[td][center]Label[img]Image15.jpg[/img][/center][/td]
[\tr]