- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试逐字符复制输入文件,然后尝试对其进行格式化以避免任何类型的对齐问题。输入文件中的第一项为 50 个字符长,第二项为 6 个字符长,第三项为 3 个字符长,第四项为 25 个字符长,第五项为 4 个字符长。我不允许使用列表或元组。以下是我的代码:
input_file=open('measles.txt','r')
f1=input('file name: ')
output_file=open(f1,'w')
for line in input_file:
newstring=''
line=line.strip()
for ch in line:
#I am trying to format the first two items in the input file
print('{0:50}{51:57}'.format(line[0:50],line[51:57]),file=output_file)
input_file.close()
output_file.close()
我收到的错误如下:
Traceback (most recent call last):
File "C:/Users/Dasinator/Documents/Books IX/Python Examples/proj07.py", line 10, in <module>
print('{0:50}{1:57}'.format(line[0:50],line[51:57]),file=output_file)
IndexError: tuple index out of range
有人可以指出代码中的错误并推荐修复方法吗?谢谢
Andorra WB_HI
Andorra WB_HI
Angola WB_LMI
Angola WB_LMI
Angola WB_LMI
输出看起来仍然不完全对齐。
这就是我修改代码的方法,但仍然遇到一些问题。
print('{0:50s}{1:6s}{2:<3s}{3:<25s}{4:<4s}'.format(line[0:50],line[50:56]),line[56:59],line[59:84],line[84:87],file=output_file)
我得到的输出是
Traceback (most recent call last):
File "C:/Users/Dasinator/Documents/Books IX/Python Examples/proj07.py", line 10, in <module>
print('{0:50s}{1:6s}{2:<3s}{3:<25s}{4:<4s}'.format(line[0:50],line[50:56]),line[56:59],line[59:84],line[84:87],file=output_file)
IndexError: tuple index out of range
最佳答案
看来您误解了 .format 的工作原理以及格式化字符串应该是什么样子。
当您编写字符串“{0:50}{1:50}”.format(stuff0, stuff1)
时,冒号将索引(之前)与字符串所包含的字符数分隔开。打印时字符串应该占据。
所以,当你这样做时:
print('{0:50}{51:57}'.format(line[0:50],line[51:57]),file=output_file)
您告诉 python 使用 .format 的第 51 个参数。您实际上的意思是 .format 的第二个参数,或字符串中的第 51 个字符。
大概,您在这里实际想要做的是打印该行的第 0-50 个字符,以及第 51-57 个字符。这看起来像:
print('{0:10}{1:10}'.format(line[0:50],line[51:57]),file=output_file)
此外,我不知道你的文件参数要做什么,但它可能不会达到你的预期。
关于python - 如何在Python中逐个复制输入文件ch并使用格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21960586/
package practicejava; public class Query { public static void main(String[] args) { char
因为我很困惑其中会存储什么。它是 ascii 值还是“z”,因为有些网站说它存储 ascii,有些网站说它存储“z”。链接说答案是“z”:http://cprogrammingworld4u.blog
这个问题在这里已经有了答案: mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expec
我无法将字符指针数组 s 的字符存储到字符数组 ch 中: int main() { char *s; s = malloc(1024 * sizeof(char));
我有这个代码: int main() { char ch[15]; cout<
在看《C++ Primer Plus 5th edition》时,看到了这样一段代码: cin.get(ch); ++ch; cout void print_type(cha
这行代码做了什么? char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) } ch:i 是什么意思? 最佳答案 这是@han solo 中提
这行代码做了什么? char_to_ix = { ch:i for i,ch in enumerate(sorted(chars)) } ch:i 是什么意思? 最佳答案 这是@han solo 中提
我在关注《C Primer Plus》这本书,遇到这样一段代码: // echo_eof.c -- repeats input to end of file. #include int main(v
这个问题在这里已经有了答案: Difference between int* a and int *a [duplicate] (2 个答案) Why is the asterisk before
我正在运行单个 goroutine 来处理与某些用户相关的消息 channel 。处理消息后,用户状态由 goroutine 更新并存储在数据库中。当对数据库的请求正在进行时,可以将许多消息发送到 c
这是完整的代码: s = 'life is short, stunt it!!?' from string import punctuation tbl = str.maketrans({ord(ch
在 C++ 中,(int) ch 是否等同于 int(ch)。 如果不是,有什么区别? 最佳答案 它们是同一个东西,也和(int)(ch)一样.在 C++ 中,通常首选使用命名转换来阐明您的意图: 使
我在 php 中使用 cURL。注意到每次启动 curl 案例时,人们总是把: $ch=curl_init(); “$ch”是什么意思? curl -F template_invoke_name='s
我正在编写与对齐相关的代码,如果给定的指针正确对齐,却没有标准的功能测试,这让我感到非常惊讶。 似乎互联网上的大多数代码都使用(long)ptr或 reinterpret_cast(ptr)测试对齐,
这个问题已经有答案了: C skipping one command of a function? [duplicate] (2 个回答) 已关闭 5 年前。 scanf("%c", &ch ) sc
然后我遇到了系统调用“write”,我尝试将“putchar”与“write”进行比较。现在,我对上面的代码行感到困惑。 最佳答案 putchar 是一个库函数。它调用 write 系统调用将字符写入
在 codechef 上用于更快输入输出的许多解决方案中,我遇到过这个表达式,但我无法理解它,因为我没有很多经验。 inline int scan( ) { int n=0; int ch=getch
这个问题在这里已经有了答案: What does space in scanf mean? [duplicate] (6 个答案) 关闭 7 年前。 像这样在scanf中加一个空格的目的是什么 sc
我的 spring-boot 应用程序中有以下 logback.xml: ... ...
我是一名优秀的程序员,十分优秀!