简体   繁体   English

使用 Pandas Python 编写 CSV 文件

[英]Writing a CSV File with Pandas Python

I did a python script to access a site, and on that site do a certain search for me to do a scan of the search result.我做了一个 python 脚本来访问一个站点,并在那个站点上进行一定的搜索让我对搜索结果进行扫描。

I write the return of the result as txt我把结果的返回写成txt

clear = self.browser.find_elements_by_class_name('Whitebackground')
for scrape in clear:
    with open('result.txt', 'a') as writer:
         writer.write(scrape.text)
         writer.write('\n')
         writer.close()

I want to return the result in CSV to open in Excel我想在 CSV 中返回结果以在 Excel 中打开

clear = self.browser.find_elements_by_class_name('Whitebackground')
    for scrape in clear:
        with open('result.csv', 'a') as writer:
             writer.write(scrape.text)
             writer.write('\n')
             writer.close()

My problem is that I have to fill 4 columns我的问题是我必须填写 4 列

I get my current result that way我以这种方式得到我目前的结果

656473930362736
The car needs to change the oil
Model: sedan
type of fuel: Gasoline

I want to receive my result in CSV in this way我想以这种方式在 CSV 中收到我的结果

'Number'; 'description'; 'Model'; 'Type of fuel'
6564...; The car needs..; sedan ; Gasoline

'Number' , 'description' , 'Model' , ' Type of fuel' would be the titles by columns 'Number' , 'description' , 'Model' , ' Type of fuel'将是列标题

'6564...' , 'The car needs...' , 'sedan' , 'Gasoline' Would be the rows of the columns '6564...' , 'The car needs...' , 'sedan' , 'Gasoline'将是列的行

Excel

does anyone have any idea how I can do this??有谁知道我该怎么做?

if you can convert your data into dictionaries, its super easy:如果您可以将数据转换为字典,则超级简单:

data = []
datapoint = {}
datapoint['Number'] = 656473930362736
datapoint['Description'] = "The car needs to change the oil."
datapoint['Model'] = "Sedan"
datapoint['Type of Fuel'] = "Gasoline"
data.append(datapoint)

fieldnames = ['Number','Description','Model','Type of Fuel']

def filewriter(filename, data, fieldnames):
        with open (filename, "w", newline='', encoding='utf-8-sig') as csvfile:
                csvfile = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=',', dialect='excel')
                csvfile.writeheader()
                for row in data:
                        csvfile.writerow(row)

filewriter("out.csv", data, fieldnames)

converting your data into dictionaries is a separate problem, but should be no big deal if your data is structured well.将您的数据转换为字典是一个单独的问题,但如果您的数据结构良好,应该没什么大不了的。

Simply parse your text by splitting into elements:只需通过拆分为元素来解析您的文本:

txt = "656473930362736\n" \
       "The car needs to change the oil\n" \
       "Model: sedan\n" \
       "type of fuel: Gasoline"

list_of_elements = txt.split('\n')
required_text = list_of_elements[0] + ';' + list_of_elements[1] + ';' list_of_elements[2].split(':')[1] + ';' + list_of_elements[3].split(':') + ';'
file.write(required_text + '\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM