Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用字典将打印输出转换为python中的字符串_Python_String_Variables_Dictionary - Fatal编程技术网

使用字典将打印输出转换为python中的字符串

使用字典将打印输出转换为python中的字符串,python,string,variables,dictionary,Python,String,Variables,Dictionary,我是Python世界的新手,希望我的问题能有一个清晰的答案。我有以下打印输出,它将值输出到控制台 # Print out the train results. print("-" * 20) print("Train number: " + str(result['trainNumber'])) print("Destination: " + str(result['locationName'])) print("Departing at: " + str(result['departure_t

我是Python世界的新手,希望我的问题能有一个清晰的答案。我有以下打印输出,它将值输出到控制台

# Print out the train results.
print("-" * 20)
print("Train number: " + str(result['trainNumber']))
print("Destination: " + str(result['locationName']))
print("Departing at: " + str(result['departure_time']))
print("Platform: " + str(result['platform']))
print("Status: " + str(result['Status']))
print("Operator: " + str(result['operator']))
print("Estimated arrival at Feltham: " + result['ETA'].strftime("%H:%M")) # Format ETA as H:M
print ("Time now: " + str(datetime.datetime.now().strftime("%H:%M")))
print("Time until train: " + str(int(result['timeToTrain'])) + " minutes")
print("Chance of getting this train: " + result['chance'])
但是,我想将它们转换为单个多行字符串,作为变量传递。这可能吗?我不确定如何处理新行-\n似乎会引起问题,我为datetimes添加的格式也会引起一些问题

我也想继续使用结果{}字典-这里最好/最干净的解决方案是什么

编辑:为了澄清一点(刚刚了解了XY问题是什么),我正在尝试在python上运行一些python代码。我在结果{}中有我需要的信息,但我需要将相同的打印值传递给单个字符串变量,以便我可以将其用作手机上工作流程的一部分。我想保持格式不变

clipboard.set(output_text)

我想我在这里要问的是什么是最干净的编码方式,因为我也在用其他格式化方法/函数修改结果{}的输出。

您可以创建一个格式字符串,从字典中查找值:

format_string = ('Train number: {trainNumber}\n'
                 'Destination: {locationName}\n'  # etc
                 'Estimated arrival at Feltham: {ETA:%H:%M}\n'
                 'Time now: {now:%H:%M}\n')  # etc
用法:

from datetime import datetime

result = {'trainNumber': 192,
          'locationName': 'Wigan',
          'ETA': datetime(2017, 9, 4, 12, 23)}

print format_string.format(now=datetime.now(),
                           **result)
输出:

列车号:192
目的地:维甘
预计抵达费尔瑟姆:12:23
现在时间:11:38


您可以创建一个格式字符串,用于从字典中查找值:

format_string = ('Train number: {trainNumber}\n'
                 'Destination: {locationName}\n'  # etc
                 'Estimated arrival at Feltham: {ETA:%H:%M}\n'
                 'Time now: {now:%H:%M}\n')  # etc
用法:

from datetime import datetime

result = {'trainNumber': 192,
          'locationName': 'Wigan',
          'ETA': datetime(2017, 9, 4, 12, 23)}

print format_string.format(now=datetime.now(),
                           **result)
输出:

列车号:192
目的地:维甘
预计抵达费尔瑟姆:12:23
现在时间:11:38


只需使用+来连接字符串。 另外,datetime.datetime.now().strftime(“%H:%M”))已经是一个字符串,因此不需要str

import pyperclip

pyperclip.copy(("-" * 20) + "\nTrain number: " + str(result['trainNumber'])   
+ "\nDestination: " + str(result['locationName']))
clipboard = pyperclip.paste()

只需使用+来连接字符串。 另外,datetime.datetime.now().strftime(“%H:%M”))已经是一个字符串,因此不需要str

import pyperclip

pyperclip.copy(("-" * 20) + "\nTrain number: " + str(result['trainNumber'])   
+ "\nDestination: " + str(result['locationName']))
clipboard = pyperclip.paste()

是的,有一种方法可以使用三个引号“%”分配给变量,示例如下

var = """
{0}
Train number: {1}
Destination: {2}
Departing at: {3}
Platform: {4}
Status: {5}""".format(("-"*20),str(result['trainNumber']),str(result['locationName']),
str(result['departure_time']), str(result['platform']), str(result['Status']))

是的,有一种方法可以使用三个引号“%”分配给变量,示例如下

var = """
{0}
Train number: {1}
Destination: {2}
Departing at: {3}
Platform: {4}
Status: {5}""".format(("-"*20),str(result['trainNumber']),str(result['locationName']),
str(result['departure_time']), str(result['platform']), str(result['Status']))

为什么要创建“作为变量传递的单个多行字符串”?你在
结果
字典中没有你需要的信息吗?
print(a)print(b)
基本上等同于
print(a+“\n”+b)
。请澄清“
\n
似乎会导致问题”的含义。还有,你确定这里没有吗?嘿,两者都有,希望在我的初始问题中得到澄清。仍然不清楚“将它们转换为单个多行字符串以作为变量传递”是什么意思。这些是什么?你正在打印的字符串?“多行字符串”是什么意思?包含换行符的字符串?一个字符串,它将所有您已打印的内容都打印到不同的行,但没有新行?为什么您要尝试创建“作为变量传递的单个多行字符串”?你在
结果
字典中没有你需要的信息吗?
print(a)print(b)
基本上等同于
print(a+“\n”+b)
。请澄清“
\n
似乎会导致问题”的含义。还有,你确定这里没有吗?嘿,两者都有,希望在我的初始问题中得到澄清。仍然不清楚“将它们转换为单个多行字符串以作为变量传递”是什么意思。这些是什么?你正在打印的字符串?“多行字符串”是什么意思?包含换行符的字符串?一个字符串,其中包含所有已打印到不同行的内容,但没有新行?