我一直试图解决这个问题,但我不能.我有以下代码:
import json
def jsonblock(filename):
my_array = []
with open(filename) as f:
for line in f:
my_array.append(line)
p = " ".join(str(x) for x in my_array)
return p;
for i in jsonblock('P5.json'):
print(i)
Run Code Online (Sandbox Code Playgroud)
而我的P5.json是
{
"signalPassed" : true,
"location" : {
"longitude" : 113.3910083760899,
"latitude" : 22.57224988908558
},
"phoneOsVersion" : "7.0.3",
"signalStdDev" : 4.139107,
"phoneModel" : "iPad",
}
Run Code Online (Sandbox Code Playgroud)
我想要str格式的正常输出,但是当我这样做时,我得到以下输出:
"
7
.
0
.
3
"
,
"
s
i
g
n
a
l
S
t
d
D
e
v
" …
Run Code Online (Sandbox Code Playgroud) 我有python的问题,我无法弄明白.我想在用户输入单词时打印一个单词的重复,然后他会告诉该单词将重复多少次.顺便说一句,我不能.这里代码到目前为止
b = raw_input 'enter word'
c = input 'enter the amount of time the word will repeat'
for g in range (c)
print (b)
Run Code Online (Sandbox Code Playgroud)
就像你看到你可以看到输入的重复但在垂直线上,我怎么能水平打印?
有人可以告诉我如何循环这些地方,而不是通过继续附加到字符串的字符串列出它.
world = ["Japan", "China", "Seattle", "Brazil"]
print "This is my story of the following places: "
for place in world:
print place
Run Code Online (Sandbox Code Playgroud)
编辑:我在打印命令的末尾添加了一个逗号,但由于它是一个单独的列表,我更喜欢,如果我能够添加一个分隔符.最终结果我希望它显示如下:
This is my store of the following places. Japan, China, Seattle, Brazil.
Run Code Online (Sandbox Code Playgroud)
最终编辑:使用以下代码,它会删除一行,我正在尝试找出如何删除附加行,以便它继续在同一行.
world = ["Japan", "China", "Seattle", "Brazil"]
print "This is my story of the following places: ",
for i in range(0,len(world)):
if i==(len(world)-1):
print world[i] + '.'
else:
print world[i] +',',
print ". Which has all
Run Code Online (Sandbox Code Playgroud)
这是我访问过以下地方的故事.他们是中国,日本,西雅图和巴西
.其中包含了所有伟大的图标
字符串的输出中有一个额外的空格。这是发生这种情况的代码部分。它发生在函数的字符串 nameConfirmation() 中。
def chooseName():
name = ""
name = raw_input("Let's begin with your name. What is it? ")
return name
def nameConfirmation():
name = chooseName()
print ("Right... So your name is", name,".")
Run Code Online (Sandbox Code Playgroud)
这是它给出的输出。
Right... So your name is Raven .
Run Code Online (Sandbox Code Playgroud)
如何去掉名字和标点符号之间的空格?
我需要在 Python 2.7 中在没有换行符的情况下将文本打印到控制台,因此我可以在以后的代码中继续在同一行上编写更多文本。我当前的实现涉及从未来库中导入 Python 3 打印函数,并使用 end=''。
这并不理想,就像我打印一行一样,例如:
print("We're doing something...",end='')
Run Code Online (Sandbox Code Playgroud)
然后使用任何其他代码,然后是一行,例如:
print("we finished doing that thing.")
Run Code Online (Sandbox Code Playgroud)
该行被打印,但它是一次打印的,这意味着它被缓冲,直到它得到包含换行符的打印。我更希望能够将第一个打印字符串输出到控制台,执行其他代码,然后放入带有换行符的部分。我无论如何都找不到在 Python 2.7 中使用打印来执行此操作的方法。也许有人可以给我指出一种功能性的方法来做到这一点?谢谢。
对于那些建议环境缓冲修复它的人来说,它没有。它会影响文件输出和其他一些与它无关的杂项。下面有一个总体上是实用的答案。
在以下代码中:
with open("output", "w") as f:
print >> f, "foo"
print >> f, "bar"
Run Code Online (Sandbox Code Playgroud)
'output'文件将是:
foo
bar
Run Code Online (Sandbox Code Playgroud)
如何避免使用换行符和空格print >>
?
PS:我其实想知道是否可以使用它print >>
.我知道其他方法可以避免'\n',例如f.write("foo")
和f.write("bar")
.
另外,我知道尾随的逗号.但那打印foo bar
,而不是foobar
.
我有这个数据
[[ 2.696000e+00 0.000000e+00 0.000000e+00 0.000000e+00]
[ 2.577000e+00 0.000000e+00 0.000000e+00 0.000000e+00]
[ 0.000000e+00 -2.560096e+03 0.000000e+00 0.000000e+00]
...
Run Code Online (Sandbox Code Playgroud)
并想为每一行打印它
2.696000e+00 & 0.000000e+00 & 0.000000e+00 & 0.000000e+00 //
2.577000e+00 & 0.000000e+00 & 0.000000e+00 & 0.000000e+00 //
...
Run Code Online (Sandbox Code Playgroud)
我所做的是
for k in range(1):
for i in range(len(data[k])):
print(data[k][i],'&')
if i == len(data[k])-1:
print(data[k][i],'//')
Run Code Online (Sandbox Code Playgroud)
哪个输出
2.696 &
0.0 &
0.0 &
0.0 &
0.0 //
Run Code Online (Sandbox Code Playgroud)
如何去掉行中每个条目之间的新行?
我正在浏览python 3的教程,我在打印时遇到了奇怪的行为.例如:
print ("\nUnpickling lists.")
pickle_file = open("pickles1.dat", "rb")
variety = pickle.load(pickle_file)
shape = pickle.load(pickle_file)
brand = pickle.load(pickle_file)
print (variety,"\n",shape,"\n",brand)
pickle_file.close()
Run Code Online (Sandbox Code Playgroud)
给我:
Unpickling lists.
['sweet', 'hot', 'dill']
['whole', 'spear', 'chip']
['Claussen', 'Heinz', 'Vlassic']
Run Code Online (Sandbox Code Playgroud)
如何避免第二个和第三个列表的打印输出行开头的额外空间?
我需要输出这个
1
22
333
4444
55555
666666
7777777
88888888
999999999
Run Code Online (Sandbox Code Playgroud)
到目前为止我只有:
def main():
for i in range(10):
for n in range(i):
print(i)
return
main()
Run Code Online (Sandbox Code Playgroud)
我得到了所有正确的数字,它没有正确格式化.如果你们可以用我的方式提出一些提示,我会很感激.
我想在python之前打印的另一个文本旁边打印一些文本
print("Hello")
a="This is a test"
print(a)
Run Code Online (Sandbox Code Playgroud)
我的意思是打印像这样的"HelloThis是一个测试"不在下一行我知道我应该使用print("Hello",a)但我想使用分离的打印命令!!!!
python ×10
python-2.7 ×4
python-3.x ×2
arrays ×1
for-loop ×1
json ×1
loops ×1
numpy ×1
output ×1
printing ×1
python-2.x ×1
string ×1
text ×1