简体   繁体   English

在python中格式化/显示列表

[英]Formatting/Displaying list in python

I'm new in python, I have a running code that extracts data from a web service and put it in a list and write it on a text file. 我是python的新手,我有一个运行中的代码,该代码从Web服务中提取数据并将其放在列表中,然后将其写入文本文件中。 When I run this code and open the text file, the data is continuous. 当我运行此代码并打开文本文件时,数据是连续的。 I need something like in every line in text file, it will display 5 elements and will go to next line and write the next 5 elements and so on. 我在文本文件的每一行中都需要类似的内容,它将显示5个元素,并转到下一行并编写下5个元素,依此类推。 Hope you can help me out on this or suggestion on other way to do this. 希望您能对此有所帮助或提出其他建议。 Here's the code. 这是代码。

f = open("test.txt", "w")

for item in test:
    f.write("%s    |    " % item)
f.close()

The result looks like this: 结果看起来像这样:

data1, data2, data3, data4, data5, data6, data7, data8......................

It should be in this format: 应采用以下格式:

data   |   data2   |   data3   |   data4   |
data5  |   data6   |   data7   |   data8   |
data9  |   data10  |   data11  |   data12  |

I hope I can have your inputs. 希望我能提供您的意见。 Thank you! 谢谢!

Those characters aren't in the list, they are the list. 这些字符不在列表 ,他们榜上有名。

>>> print ''.join(['foo', 'bar', 'baz'])
foobarbaz

As @Ignacio said, you need to change the way you print the list. 正如@Ignacio所说,您需要更改打印列表的方式。

for line in list_s:
    # f.write("%s\n" % line) 
    f.write(''.join(line))

If you'd like to display it in a table, you can format each row like this: 如果要将其显示在表格中,则可以像这样格式化每一行:

>>> print '{0:10s} {1:10s} {2:10s}'.format('foo', 'bar', 'baz')
foo        bar        baz       

Or with lines: 或用线条:

>>> print '{0:10s} | {1:10s} | {2:10s}'.format('foo', 'bar', 'baz')
foo        | bar        | baz    

See the Python Input and Output documentation for more information 有关更多信息,请参见Python 输入和输出文档

for i, item in enumerate(test):
    if i and i % 4 == 0:
        f.write("\n")
    f.write("%-9s|   " % item)

What do you call "special characters"? 您怎么称呼“特殊字符”? What is the encoding of the data you have? 您所拥有数据的编码是什么? Before proceeding, it is important for that you do understand what are encodings, what is unicode, and that the wrold, and data you get around is not restricted to the 26 letters primarily used in the English language. 在继续之前,重要的是您必须了解什么是编码,什么是unicode,并且所得到的信息和数据不限于英语中主要使用的26个字母。 Check this article to learn about it. 查看这篇文章以了解它。

That said, Python can encode your data in an encoding of your choice - even ASCII, so you only get the characters 32-127, and no "stranger" characters at all - like ã, ç, É - or you can encode your data in utf-8 to be able to use the whole array of characters we use around the planet. 也就是说,Python可以使用您选择的编码(甚至是ASCII)对数据进行编码,因此您只能获得32-127个字符,而根本没有“陌生人”字符(如ã,ç,É),或者可以对数据进行编码在utf-8中可以使用我们在地球上使用的整个字符数组。 However, if you can only "encode" Unicode data (Python tries to implicit convert a byte string to unicode before if you use the encode string method) - so you do have to know the encoding of your data source anyway. 但是,如果您只能“编码” Unicode数据(如果使用编码字符串方法,Python会尝试先将字节字符串隐式转换为unicode)-因此,无论如何,您确实必须知道数据源的编码。 Once your data is decoded into unicode (inside your running program), you can encode it to your desired output (eg ASCII or "quopri_codec" ), and set the "errors" keyword parameter to "ignore" or "xmlcharrefreplace" on your call to encode. 一旦将数据解码为unicode(在正在运行的程序内部),就可以将其编码为所需的输出(例如ASCII或“ quopri_codec”),并在调用时将“ errors”关键字参数设置为“ ignore”或“ xmlcharrefreplace”进行编码。

To understand it better, check the codecs documentation from Python 为了更好地理解它,请查看Python的编解码器文档

For example, assuming your input data is incoded in iso8859_15 (aka latin1 or cp1252 put or take 2 char definitions),a nd that you really mean to suppress any non-English character: 例如,假设您的输入数据是用iso8859_15编码的(又名latin1或cp1252 put或take 2个char定义),那么您的意思就是禁止显示任何非英语字符:

s = client.service.GetData('data1')
s = s.decode("iso88159_15").encode("ASCII", errors="ignore")
sr = '<root>%s</root>' % s
root = ET.fromstring(sr)

Otoh, since you are encoding xml, putting xml references to the chars you don't want to deal with should not hurt - (as probably encoding to utf-8 would not hurt either): Otoh,因为您正在编码xml,所以将xml引用放置到您不想处理的字符上应该不会受到伤害-(因为编码为utf-8可能不会受到伤害):

s = s.decode("iso88159_15").encode("ASCII", errors="xmlcharrefreplace")

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

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