简体   繁体   English

如何使用CSV文件中的数据在txt文件中写入行

[英]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: 我所做的进一步更改:

  • In Python 2, open a CSV files in binary mode, the csv module handles line endings itself, because correctly quoted column data can have embedded newlines in them. 在Python 2中,以二进制模式打开CSV文件, csv模块会自行处理行尾,因为正确引用的列数据可以在其中嵌入换行符。
  • Regular text files should be opened in text mode still though. 常规文本文件仍应以文本模式打开。
  • When writing lines to a file, do remember to add a newline character to delineate lines. 将行写入文件时,请记住要添加换行符来描述行。
  • Using a string format ( 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.

相关问题 如何将多行 Reddit 数据(使用 praw)写入 csv/txt 文件? - How to write multiple lines of Reddit data (using praw) to csv/txt file? 如何编写 python 从名为“file1.txt”的文本文件中读取前两行 将从“file1.txt”读取的两行写入新文件“file2.txt” - How write python to Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt" 将文本文件中的行写入.csv文件 - Write lines from text file into .csv file 如何使用另一个 csv 文件中的数据写入现有的 CSV 文件? - How to write in an existing CSV file with data from another csv file? 如何读取字典行并写入CSV文件 - How to read lines of dictionary and write to a CSV file 如何使用 %BASH 将包含特定字符串的行从 a.csv 文件复制到 Python 中的 a.txt 文件? - How do you copy lines that contain specific strings from a .csv file to a .txt file in Python using %BASH? 如果它们存在于另一个 txt 文件中,如何从 txt 文件中删除它们 - how to remove lines from a txt file if they exist on an another txt file pandas .txt 文件到 .csv 文件缺少行 - pandas .txt file to .csv file missing lines 将非结构化 TXT 文件中的数据移动到 CSV - Moving Data into a CSV from unstructured TXT file 从.txt文件中混淆.csv中的数据 - Obfuscate Data in .csv from a .txt file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM