我正在将文件(php)解析为C#中的列表,以便我可以使用它来完成任务。我之前在PHP中已经完成了它,但它的工作速度非常慢,所以我正在尝试制作一个C#winform应用程序。
这是我在C#中已有的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace g7_product_parser
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void log(String msg)
{
textBox1.AppendText(msg + "\r\n\r\n");
}
public string[] parseLine(String regel)
{
string[] parsed;
parsed = new string[2];
regel = regel.Replace("array(", "");
regel = regel.Replace("),", "");
regel = regel.Replace(")", "");
string[] temp = regel.Split(',');
foreach (string part in temp)
{
string[] part2 = Regex.Split(part, "=>");
// log(String.Join(",", part2));
if (part2[0].Replace("'", "").Trim() == "keyword_uid")
{
parsed[1] = part2[1].Replace("'", "").Trim();
}
else if (part2[0].Replace("'", "").Trim() == "product_uid")
{
parsed[0] = part2[1].Replace("'", "").Trim();
}
}
return parsed;
}
private void button1_Click(object sender, EventArgs e)
{
String startTime = DateTime.Now.ToString("HH:mm:ss tt");
log("Begin!");
string[] lines = System.IO.File.ReadAllLines("tx_ttproductsv2_keywords_in_products.php");
List<string> entrys = new List<string>();
foreach (string line in lines)
{
// parseLine(line);
log(String.Join(",",parseLine(line)));
Application.DoEvents();
}
String endTime = DateTime.Now.ToString("HH:mm:ss tt");
String totalTime = (DateTime.Parse(endTime) - DateTime.Parse(startTime)).ToString();
log("Klaar!");
log("Start Tijd: " + startTime + "\r\nEind Tijd: " + endTime + "\r\nTotale Tijd: " + totalTime);
}
}
}
要解析的文件如下所示:
array('uid' => '1','pid' => '2','keyword_uid' => '1','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '2','pid' => '2','keyword_uid' => '2','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '3','pid' => '2','keyword_uid' => '3','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '4','pid' => '2','keyword_uid' => '4','product_uid' => '1','amount' => '9','in_title' => '0'),
array('uid' => '5','pid' => '2','keyword_uid' => '5','product_uid' => '1','amount' => '18','in_title' => '0'),
array('uid' => '6','pid' => '2','keyword_uid' => '6','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '7','pid' => '2','keyword_uid' => '7','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '8','pid' => '2','keyword_uid' => '8','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '9','pid' => '2','keyword_uid' => '9','product_uid' => '1','amount' => '79','in_title' => '1'),
array('uid' => '10','pid' => '2','keyword_uid' => '10','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '11','pid' => '2','keyword_uid' => '11','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '12','pid' => '2','keyword_uid' => '12','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '13','pid' => '2','keyword_uid' => '13','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '14','pid' => '2','keyword_uid' => '14','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '15','pid' => '2','keyword_uid' => '15','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '16','pid' => '2','keyword_uid' => '16','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '17','pid' => '2','keyword_uid' => '17','product_uid' => '1','amount' => '12','in_title' => '0'),
array('uid' => '18','pid' => '2','keyword_uid' => '18','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '19','pid' => '2','keyword_uid' => '19','product_uid' => '1','amount' => '15','in_title' => '1'),
array('uid' => '20','pid' => '2','keyword_uid' => '20','product_uid' => '1','amount' => '6','in_title' => '0'),
array('uid' => '21','pid' => '2','keyword_uid' => '21','product_uid' => '1','amount' => '15','in_title' => '0'),
array('uid' => '22','pid' => '2','keyword_uid' => '22','product_uid' => '1','amount' => '21','in_title' => '0'),
array('uid' => '23','pid' => '2','keyword_uid' => '23','product_uid' => '1','amount' => '12','in_title' => '0'),
我有一些像这样的PHP代码:
require_once 'export/tx_ttproductsv2_keywords_in_products_part.php';
$sql = new Sql;
$tagList = array();
$counter = 0;
$counter2 = 0;
$counter3 = 0;
foreach ($tx_ttproductsv2_keywords_in_products as $key => $value) {
$keywordsList = $sql -> getKeyWords($value['keyword_uid']);
foreach ($keywordsList as $key2 => $value2) {
if(isset($tagList[$value['product_uid']])){
$tagList[$value['product_uid']] .= ", " . $value2['keyword'];
}else{
$tagList[$value['product_uid']] = $value2['keyword'];
}
}
}
以这种方式(在PHP中),我检查数组中的条目是否已经被激活,如果这是真的,则追加,或者创建一个新条目。
我的问题是:我怎样才能在C#中做到这一点。我认为我必须使用一个列表,但我真的不知道究竟是怎么回事。
答案 0 :(得分:2)
我编写了一个快速而又脏的控制台程序,将其转换为数组。看看你是否可以根据自己的目的进行修改......如果没有,请详细说明你需要它做什么。
(注意:这是一个快速的工作,并不是处理这个问题的最有效方法。)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace simplearraything
{
class myarrayitem
{
public int uid;
public int pid;
public int keyword_uid;
public int product_uid;
public int amount;
public int in_title;
}
class Program
{
static void Main(string[] args)
{
//array('uid' => '23','pid' => '2','keyword_uid' => '23','product_uid' => '1','amount' => '12','in_title' => '0'),
string Original = File.ReadAllText("array.txt").Replace("array('uid' => '", "").Replace(
"','pid' => '", "/").Replace("','keyword_uid' => '", "/").Replace("','product_uid' => '", "/").Replace(
"','amount' => '", "/").Replace("','in_title' => '", "/").Replace("'),", "").Replace("\r","");
string[] lines = Original.Split('\n');
List<myarrayitem> mystuff = new List<myarrayitem>();
for (int i = 0; i < lines.Length; i++)
{
string[] thisitem = lines[i].Split('/');
myarrayitem item = new myarrayitem();
item.uid = int.Parse(thisitem[0]);
item.pid = int.Parse(thisitem[1]);
item.keyword_uid = int.Parse(thisitem[2]);
item.product_uid = int.Parse(thisitem[3]);
item.amount = int.Parse(thisitem[4]);
item.in_title = int.Parse(thisitem[5]);
mystuff.Add(item);
}
// Now, output it to prove accuracy:
for (int i = 0; i < lines.Length; i++)
{
Console.WriteLine("UID:" + mystuff[i].uid.ToString() + "/PID:" + mystuff[i].pid.ToString() + "/KEYWORD_UID:" + mystuff[i].keyword_uid.ToString() +
"/PRODUCT_UID:" + mystuff[i].product_uid.ToString() + "/amount:" + mystuff[i].amount.ToString() + "/in_title:" + mystuff[i].in_title.ToString());
}
Console.Read();
}
}
}
回应评论:
List<myarrayitem> items new List<myarrayitem>();
for (int i = 0;i < mystuff.Count;i++)
{
if (mystuff[i].product_uid == PRODUCT_UID_HERE)
{
items.add(mystuff[i]);
// Or, instead of this items List, you could just do whatever you wanted to do with the items right here.
}
}
答案 1 :(得分:1)
试试这个:
public class DataArray { public string uid { get; set; } public string pid { get; set; } public string keyword_uid { get; set; } public string product_uid { get; set; } public string amount { get; set; } public string in_title { get; set; } } public class RootObject { public DataArray DataArray { get; set; } }
//接下来的4行将该数据转换为JSON格式
toProcess = toProcess.Replace(“array(”,“{\”DataArray \“:{”);
toProcess = toProcess.Replace(“)”,“}}”);
toProcess = toProcess.Replace(“'”,“\”“);
toProcess =“[”+ toProcess.Replace(“=&gt;”,“:”)+“]”;List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(toProcess); foreach ( RootObject d in data ) { // do something here like.. if (d.DataArray.uid == "1") { // } }