python:合并两个csv文件 [英] python: merge two csv files

查看:537
本文介绍了python:合并两个csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当我使用python做我的任务。
我是新来的python,所以我是一个完整的初学者。

I have a problem while I'm doing my assignment with python. I'm new to python so I am a complete beginner.

问题:如何合并两个文件?

Question: How can I merge two files below?

s555555,7
s333333,10
s666666,9
s111111,10
s999999,9

s111111,,,,,
s222222,,,,,
s333333,,,,,
s444444,,,,,
s555555,,,,,
s666666,,,,,
s777777,,,,,

合并后应如下所示:

s111111,10,,,,
s222222,,,,,
s333333,10,,,,
s444444,,,,,
s555555,7,,,,
s666666,9,,,,
s777777,,,,,
s999999,9,,,,

感谢阅读,任何帮助将不胜感激! !

Thanks for reading and any helps would be appreciated!!!

推荐答案

这里是您可以采取的一个方法来解决问题的步骤。在这里,我将使用 FileA FileB 结果作为各种文件名。

Here are the steps you can follow for one approach to the problem. In this I'll be using FileA, FileB and Result as the various filenames.

一种解决问题的方法是给文件中的每个位置(每个)一个数字来引用它,然后你从 FileA 中读取行,那么你知道在第一个之后,您需要将 FileB 中的第一行置入您要写入 Result 的结果。 p>

One way to approach the problem is to give each position in the file (each ,) a number to reference it by, then you read the lines from FileA, then you know that after the first , you need to put the first line from FileB to build your result that you will write out to Result.


  1. 打开 FileA 。理想情况下,您应该使用 with语句 ,因为它会在文件完成后自动关闭。或者您可以使用正常的 open()调用,但确保在完成后关闭文件。

  1. Open FileA. Ideally you should use the with statement because it will automatically close the file when its done. Or you can use the normal open() call, but make sure you close the file after you are done.

循环遍历 FileA 的每一行,并将其添加到列表。 (提示:你应该使用 split())。为什么要列表?

Loop through each line of FileA and add it to a list. (Hint: you should use split()). Why a list? It makes it easier to refer to items by index as that's our plan.

重复步骤1和2 FileB ,但将它存储在不同的列表变量中。

Repeat steps 1 and 2 for FileB, but store it in a different list variable.

现在下一部分是循环列表 FileA 中的行匹配来自 FileB 的列表,以创建一个新行,您将写入结果文件。你可以用很多方法,但一个简单的方法是:

Now the next part is to loop through the list of lines from FileA, match them with the list from FileB, to create a new line that you will write to the Result file. You can do this many ways, but a simple way is:


  1. 首先创建一个空列表来存储你的结果( final_lines = []

  2. 循环遍历列表中的 FileA for 循环。

  1. First create an empty list that will store your results (final_lines = [])
  2. Loop through the list that has the lines for FileA in a for loop.

您还应该记住, FileA 将在 FileB 中具有相应的行。对于 FileA 的列表中的每个第一个位,在 FileB 的列表中找到相应的行,然后使用 index()获取下一个项目。如果你渴望你会意识到第一个项目总是 0 ,下一个总是 1 ,所以为什么不简单地硬编码的值?如果你看看作业;有多个,所以可能在某个时候你有一个第四或第五个列需要添加。

You should also keep in mind that not every line from FileA will have a corresponding line in FileB. For every first "bit" in FileA's list, find the corresponding line in FileB's list, and then get the next item by using the index(). If you are keen you would have realized that the first item is always 0 and the next one is always 1, so why not simply hard code the values? If you look at the assignment; there are multiple ,s so it could be that at some point you have a fourth or fifth "column" that needs to be added. Teachers love to check for this stuff.


  1. 使用 append()添加项目的正确顺序为 final_lines

  1. Use append() to add the items in the right order to final_lines.

最后一部分很简单:


  1. 打开一个新文件(使用 open

  2. 循环通过 final_lines

  3. 将每一行写入文件(确保您不要忘记行尾字符)。

  4. 关闭文件。

  1. Open a new file (use with or open)
  2. Loop through final_lines
  3. Write each line out to the file (make sure you don't forget the end of line character).
  4. Close the file.

如果您有任何特定问题,请询问。

If you have any specific questions - please ask.

这篇关于python:合并两个csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆