,与python中的文件阅读和写作有关的问题

发布于 2025-02-12 06:26:25 字数 1316 浏览 0 评论 0原文

我想将文件中的每一行反向,然后将其写入另一个文件。以我的结果,一开始有一个空间,结果的最后两行是连接的。这是我下面的代码。非常感谢!

with open("D:\\Users\\Documents\\Documents\\pythonFile\\PracticeAboutPython123\\resource\\data.csv") as fi,\
        open("D:\\Users\\Documents\\Documents\\pythonFile\\PracticeAboutPython123\\resource\\reverseline.txt","w+") as fo:
    txt=fi.readlines()
    for line in txt:
        line = line[::-1]
        fo.write(line)

这是原始文件:

1,2,3,4,5,6,7
8, 3, 2, 7, 1, 4, 6, 5
6, 1, 3, 8, 5, 7, 4, 2
'a','b','c','x','y','z','i','j','k'
'k', 'b', 'j', 'c', 'i', 'y', 'z', 'a', 'x'
'z', 'c', 'b', 'a', 'k', 'i', 'j', 'y', 'x'
'a', 'y', 'b', 'x', 'z', 'c', 'i', 'j', 'k'
5, 2, 4, 7, 1, 6, 8, 3

这是我的错误结果:

(A blank line in there)
7,6,5,4,3,2,1
5 ,6 ,4 ,1 ,7 ,2 ,3 ,8
2 ,4 ,7 ,5 ,8 ,3 ,1 ,6
'k','j','i','z','y','x','c','b','a'
'x' ,'a' ,'z' ,'y' ,'i' ,'c' ,'j' ,'b' ,'k'
'x' ,'y' ,'j' ,'i' ,'k' ,'a' ,'b' ,'c' ,'z'
'k' ,'j' ,'i' ,'c' ,'z' ,'x' ,'b' ,'y' ,'a'3 ,8 ,6 ,1 ,7 ,4 ,2 ,5

这是我想要的结果:

7,6,5,4,3,2,1
5 ,6 ,4 ,1 ,7 ,2 ,3 ,8
2 ,4 ,7 ,5 ,8 ,3 ,1 ,6
'k','j','i','z','y','x','c','b','a'
'x' ,'a' ,'z' ,'y' ,'i' ,'c' ,'j' ,'b' ,'k'
'x' ,'y' ,'j' ,'i' ,'k' ,'a' ,'b' ,'c' ,'z'
'k' ,'j' ,'i' ,'c' ,'z' ,'x' ,'b' ,'y' ,'a'
3,8,6,1,7,4,2,5

I want to reverse each line in a file and write it in another file. In my result, there with a space at the beginning, and the last two lines of the result are concatenated. This is my code below.Thanks a lot!

with open("D:\\Users\\Documents\\Documents\\pythonFile\\PracticeAboutPython123\\resource\\data.csv") as fi,\
        open("D:\\Users\\Documents\\Documents\\pythonFile\\PracticeAboutPython123\\resource\\reverseline.txt","w+") as fo:
    txt=fi.readlines()
    for line in txt:
        line = line[::-1]
        fo.write(line)

This is origin file:

1,2,3,4,5,6,7
8, 3, 2, 7, 1, 4, 6, 5
6, 1, 3, 8, 5, 7, 4, 2
'a','b','c','x','y','z','i','j','k'
'k', 'b', 'j', 'c', 'i', 'y', 'z', 'a', 'x'
'z', 'c', 'b', 'a', 'k', 'i', 'j', 'y', 'x'
'a', 'y', 'b', 'x', 'z', 'c', 'i', 'j', 'k'
5, 2, 4, 7, 1, 6, 8, 3

This is my incorrect result:

(A blank line in there)
7,6,5,4,3,2,1
5 ,6 ,4 ,1 ,7 ,2 ,3 ,8
2 ,4 ,7 ,5 ,8 ,3 ,1 ,6
'k','j','i','z','y','x','c','b','a'
'x' ,'a' ,'z' ,'y' ,'i' ,'c' ,'j' ,'b' ,'k'
'x' ,'y' ,'j' ,'i' ,'k' ,'a' ,'b' ,'c' ,'z'
'k' ,'j' ,'i' ,'c' ,'z' ,'x' ,'b' ,'y' ,'a'3 ,8 ,6 ,1 ,7 ,4 ,2 ,5

This is the result I want:

7,6,5,4,3,2,1
5 ,6 ,4 ,1 ,7 ,2 ,3 ,8
2 ,4 ,7 ,5 ,8 ,3 ,1 ,6
'k','j','i','z','y','x','c','b','a'
'x' ,'a' ,'z' ,'y' ,'i' ,'c' ,'j' ,'b' ,'k'
'x' ,'y' ,'j' ,'i' ,'k' ,'a' ,'b' ,'c' ,'z'
'k' ,'j' ,'i' ,'c' ,'z' ,'x' ,'b' ,'y' ,'a'
3,8,6,1,7,4,2,5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2025-02-19 06:26:36

您从输入文件中读取的每一行都以newline字符('\ n')结束。因此,当您扭转字符时,新线就在开始。

所以:

with open('input.txt') as infile, open('output.txt', 'w+') as outfile:
    for line in infile.readlines():
      rev = line[::-1]
      o = rev[0] == '\n'
      print(rev[o:], file=outfile)

Each line that you read from the input file ends with a newline character ('\n'). Therefore, when you reverse the characters the newline is at the beginning.

Therefore:

with open('input.txt') as infile, open('output.txt', 'w+') as outfile:
    for line in infile.readlines():
      rev = line[::-1]
      o = rev[0] == '\n'
      print(rev[o:], file=outfile)
江南烟雨〆相思醉 2025-02-19 06:26:34

我有一些灵感。因为line = line [:: - 1],因此结果文件具有与原始文件相同的列表元素数量。
因为开头有一个空线,所以最后一行没有位置,因此它连接到上线,从而导致了此结果。
好吧,所以为什么有一个空线以及如何将其删除。

I have some inspiration. Because line = line[::-1], so The resulting file has the same number of list elements as the original file.
Because there is an empty line at the beginning, so the last line has no place, so it is connected to the previous line, resulting in this result.
well,so why there is an empty line and how can I remove it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文