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
.
-
打开
FileA
。理想情况下,您应该使用with语句
,因为它会在文件完成后自动关闭。或者您可以使用正常的open()
调用,但确保在完成后关闭文件。
Open
FileA
. Ideally you should use thewith statement
because it will automatically close the file when its done. Or you can use the normalopen()
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:
- 首先创建一个空列表来存储你的结果(
final_lines = []
) - 循环遍历列表中的
FileA
for
循环。
- First create an empty list that will store your results (
final_lines = []
) - Loop through the list that has the lines for
FileA
in afor
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.
- 使用
append()
添加项目的正确顺序为final_lines
。
- Use
append()
to add the items in the right order tofinal_lines
.
最后一部分很简单:
- 打开一个新文件(使用
和
或open
) - 循环通过
final_lines
- 将每一行写入文件(确保您不要忘记行尾字符)。
- 关闭文件。
- Open a new file (use
with
oropen
) - Loop through
final_lines
- Write each line out to the file (make sure you don't forget the end of line character).
- Close the file.
如果您有任何特定问题,请询问。
If you have any specific questions - please ask.