[英]How to write lines in a txt file, with data from a csv file
How can I tell Python to open a CSV file, and merge all columns per line, into new lines in a new TXT file? 如何告诉Python打开CSV文件并将每行的所有列合并为新TXT文件中的新行?
To explain: 解释:
I'm trying to download a bunch of member profiles from a website, for a research project. 我正在尝试从网站上下载大量会员资料,以进行研究项目。 To do this, I want to write a list of all the URLs in a TXT file.
为此,我想在TXT文件中写入所有URL的列表。
The URLs are akin to this: website.com-name-country-title-id.html 这些URL类似于此:website.com-name-country-title-id.html
I have written a script that takes all these bits of information for each member and saves them in columns (name/country/title/id), in a CSV file, like this: 我编写了一个脚本,该脚本将每个成员的所有这些信息位保存在CSV文件的列(名称/国家/标题/ id)中,如下所示:
mark japan rookie married
john sweden expert single
suzy germany rookie married
etc...
Now I want to open this CSV and write a TXT file with lines like these: 现在,我想打开此CSV并使用以下行编写TXT文件:
www.website.com/mark-japan-rookie-married.html
www.website.com/john-sweden-expert-single.html
www.website.com/suzy-germany-rookie-married.html
etc...
Here's the code I have so far. 这是我到目前为止的代码。 As you can probably tell I barely know what I'm doing so help will be greatly appreciated!!!
如您所知,我几乎不知道自己在做什么,因此将不胜感激帮助!!!
import csv
x = "http://website.com/"
y = ".html"
csvFile=csv.DictReader(open("NameCountryTitleId.csv")) #This file is stored on my computer
file = open("urls.txt", "wb")
for row in csvFile:
strArgument=str(row['name'])+"-"+str(row['country'])+"-"+str(row['title'])+"-"+str(row['id'])
try:
file.write(x + strArgument + y)
except:
print(strArgument)
file.close()
I don't get any error messages after running this, but the TXT file is completely empty. 运行此命令后没有收到任何错误消息,但TXT文件完全为空。
Rather than using a DictReader
, use a regular reader to make it easier to join the row: 而不是使用
DictReader
,而是使用常规阅读器可以更轻松地加入行:
import csv
url_format = "http://website.com/{}.html"
csv_file = 'NameCountryTitleId.csv'
urls_file = 'urls.txt'
with open(csv_file, 'rb') as infh, open(urls_file, 'w') as outfh:
reader = csv.reader(infh)
for row in reader:
url = url_format.format('-'.join(row))
outfh.write(url + '\n')
The with
statement ensures the files are closed properly again when the code completes. with
语句可确保在代码完成后再次正确关闭文件。
Further changes I made: 我所做的进一步更改:
csv
module handles line endings itself, because correctly quoted column data can have embedded newlines in them. csv
模块会自行处理行尾,因为正确引用的列数据可以在其中嵌入换行符。 str.format()
) is far more flexible than using string concatenations. str.format()
)比使用字符串串联更加灵活。 str.join()
lets you join a sequence of strings together with a separator. str.join()
使您可以将字符串序列与分隔符连接在一起。 its actually quite simple, you are working with strings yet the file you are opening to write to is being opened in bytes mode, so every single time the write fails and it prints to the screen instead. 它实际上非常简单,您正在使用字符串,但是要打开写入的文件却以字节模式打开,因此每次写入失败并打印到屏幕上。 try changing this line:
尝试更改此行:
file = open("urls.txt", "wb")
to this: 对此:
file = open("urls.txt", "w")
EDIT: i stand corrected, however i would like to point out that with an absence of newlines or some other form of separator, how do you intend to use the URLs later on? 编辑:我的立场是正确的,但是我想指出的是,由于缺少换行符或其他某种形式的分隔符,以后打算如何使用这些URL? if you put newlines between each URL they would be easy to recover
如果在每个URL之间插入换行符,则很容易恢复
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.