python在 【按我这种样子】输出至文本的时候如何对齐(注意看内容,有两个问题)?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python在 【按我这种样子】输出至文本的时候如何对齐(注意看内容,有两个问题)?相关的知识,希望对你有一定的参考价值。

解释一下,第一张图 最左边的是格式化输出,占了30个位置 然后加上四个制表符 再输出右边的内容,结果总体整齐,但是有一小部分不行第二张图就左边换成了25个字符,但是就很不整齐。我想知道这是怎么回事(比如说制表符和空格对齐有什么机制啊),以及如果想要按我这种要求对齐的话,如何去做?

这个需要测试的,tab符的宽度,不同的阅读工具是不一样的,效果也就不同。常见的有4和8个字符两种。

举个例子,我这个tab符宽度是4个字符

第1行,4个字符  +  4个tab符X4 = 20宽度

第2行,4个字符 + "路"和tab符一共4个字符 = 8 + 剩下3个tab符X4=20宽度

第3行:"最近"4个字符 +  两个逗号和“近来”6个字符 + “最近时”6个字符 = 16个字符 + 最后一个“间”+上tab符4个字符= 20宽度

所以3行可以对齐

追问

所以我想要对齐,就是要去判断最左边的字符有多少个,然后根据这个来确定制表符的个数对吗

追答

要参照最长的字符串,然后就是要确定查看的程序,tab符的宽度

参考技术A

文字就是题目的话,保存前要保证每个字符串都strip()

python 更新文本文件内容

问题背景:使用python从一个文本文件中读取数据,然后经过处理再输出到一个新建立的文本文件里
问题关键:当再次运行这个python程序的时候,新的文字内容就会覆盖掉原有的内容,请问,如何才能避免这种情况?就是说,我只是想往文本文件里插入新的内容,并不想覆盖掉原有的内容。
注明:我是初学者,别搞出一大堆乱七八糟的东西来,我相信这个问题没有那么难。还有,别从别处复制粘贴过来,我已经在网上找了整整一天了,能解决的话我就不到这里来问了。
import os

def main():
filename=raw_input('Enter filename:')
path=os.getcwd()
f=open('%s\\%s'%(path,filename),'r')
minimum=maximum=float(f.readline())
summ=0.0
n=0
for eachline in f:
if float(eachline)<minimum:
minimum=float(eachline)
if float(eachline)>maximum:
maximum=float(eachline)
summ+=float(eachline)
n+=1
avg=summ/n
maximum=str(maximum)
minimum=str(minimum)
summ=str(summ)
avg=str(avg)
stats=open('stats.txt','w+')
stats.write(filename+'-'+' Max: '+maximum+' Min: '+minimum+' Sum: '+summ+' Avg: '+avg)
main()
以上这个是我写的代码,虽然我加入了w+,但是,结果仍然是,每次我运行程序,stats.txt这里文件里原有的内容就会被覆盖掉。

请详细阅读文档,使用a+的选项打开。

open(filename[, mode[, bufsize]])¶

Open a file, returning an object of the file type described in section File Objects. If the file cannot be opened, IOError is raised. When opening a file, it’s preferable to use open() instead of invoking the file constructor directly.

The first two arguments are the same as for stdio‘s fopen(): filename is the file name to be opened, and mode is a string indicating how the file is to be opened.

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

The optional bufsize argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative bufsize means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used. [2]

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.

In addition to the standard fopen() values mode may be 'U' or 'rU'. Python is usually built with universal newline support; supplying 'U' opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention '\r\n'. All of these external representations are seen as '\n' by the Python program. If Python is built without universal newline support a mode with 'U' is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types seen.

Python enforces that the mode, after stripping 'U', begins with 'r', 'w' or 'a'.

Python provides many file handling modules including fileinput, os, os.path, tempfile, and shutil.

Changed in version 2.5: Restriction on first letter of mode string introduced.

ord(c)¶
参考技术A

Python具有基本的文本文件读写功能,文本文件的读写更新主要通过open()所构建的文件对象来实现。

1.创建文件对象:

f = open(文件名,模式)

2.最常用的模式有

"r"     # 只读
"w"     # 写入

3.文件对象的方法

#读取
content = f.read(N)          # 读取N bytes的数据  
content = f.readline()       # 读取一行 
content = f.readlines()      # 读取所有行,储存在列表中,每个元素是一行。
#写入
f.write('I like apple')      # 将'I like apple'写入文件
#关闭文件
f.close()

参考技术B f = file("a.txt","w+")
python操作文件的时候可以用open,也可以用file,在这两个方法中都加入mode选择是追加还是覆盖。
不知道你有没有python shell环境
你敲个help(file)就看到说明了
参考技术C from __future__ import with_statement # for python2.5
import os
import os.path

def main():
filename = raw_input('Enter filename:')
minimum = maximum = n = 0
summ = 0.0
with open(os.path.join(os.getcwd(), filename)) as f:
for line in f:
value = float(line)
if value < minimum:
minimum = value
elif value > maximum:
maximum = value

summ += value
n += 1
avg = summ / n
with open('stats.txt', 'a') as f:
f.write('%s- Max: %f Min: %f Sum: %f Avg: %f' % (filename, maximum, minimum, summ, avg))

# 楼主只要从第二行到最后的平均值和总和?我的代码里包括了从第一行到最后的值,不知道这是你的真正需求还是误解
# 使用'a'模式打开会把文件游标定位到最后以便插入新值
参考技术D 把w+ 改成a

以上是关于python在 【按我这种样子】输出至文本的时候如何对齐(注意看内容,有两个问题)?的主要内容,如果未能解决你的问题,请参考以下文章

如何用WORD2007快速制作树状图总结

python 输出乘法式子 输入两个整数,输出如输出样例所示的乘法式子?

Python 插入长文本至Oracle

hive 导出数据到本地

Java使用Runtime.getRuntime()去调用python时候,不能够实时输出python里面的print输出

Python将控制台输出保存至文件(loguru)